SlideShare a Scribd company logo
1
Structures
2
What is a Structure?
 Used for handling a group of logically
related data items
Examples:
 Student name, roll number, and marks
 Real part and complex part of a complex number
 Helps in organizing complex data in a more
meaningful way
 The individual structure elements are called
members
3
Defining a Structure
struct tag {
member 1;
member 2;
:
member m;
};
 struct is the required C keyword
 tag is the name of the structure
 member 1, member 2, … are individual member
declarations
4
Contd.
 The individual members can be ordinary variables,
pointers, arrays, or other structures (any data type)
 The member names within a particular structure
must be distinct from one another
 A member name can be the same as the name of
a variable defined outside of the structure
 Once a structure has been defined, the individual
structure-type variables can be declared as:
struct tag var_1, var_2, …, var_n;
5
Example
 A structure definition
struct student {
char name[30];
int roll_number;
int total_marks;
char dob[10];
};
 Defining structure variables:
struct student a1, a2, a3;
A new data-type
6
A Compact Form
 It is possible to combine the declaration of the structure
with that of the structure variables:
struct tag {
member 1;
member 2;
:
member m;
} var_1, var_2,…, var_n;
 Declares three variables of type struct tag
 In this form, tag is optional
7
Initialization of Structures
8
Structure Initialization
 Structure variables may be initialized following
similar rules of an array. The values are provided
within the second braces separated by commas
 An example:
struct complex a={1.0,2.0}, b={-3.0,4.0};
a.real=1.0; a.imag=2.0;
b.real=-3.0; b.imag=4.0;
9
Accessing a Structure
 The members of a structure are processed
individually, as separate entities
 Each member is a separate variable
 A structure member can be accessed by writing
variable.member
where variable refers to the name of a structure-type
variable, and member refers to the name of a
member within the structure
 Examples:
a1.name, a2.name, a1.roll_number, a3.dob
10
Example: Complex number addition
void main()
{
struct complex
{
float real;
float cmplex;
} a, b, c;
scanf (“%f %f”, &a.real, &a.cmplex);
scanf (“%f %f”, &b.real, &b.cmplex);
c.real = a.real + b.real;
c.cmplex = a.cmplex + b.cmplex;
printf (“n %f + %f j”, c.real, c.cmplex);
}
11
Operations on Structure
Variables
 Unlike arrays, a structure variable can be directly
assigned to another structure variable of the same type
a1 = a2;
 All the individual members get assigned
 Two structure variables can not be compared for
equality or inequality
if (a1 == a2)…… this cannot be done
12
#include<stdio.h>
struct
employee
{
int emp_id;
char name[10];
double salary;
char designation [20];
float experience;
};
void main()
{
struct employee e;
printf(“%d”,sizeof(e));
}
13
: C program to read and display the student details using structures.
#include<stdio.h>
struct student
{
int rnum;
char name[20];int marks;
}s[60];
void main()
{
int i,n;
printf("Enter the number of students");
scanf("%d",&n); printf("nEnter the roll number, Name , Marksn");
for(i=1;i<=n;i++)
{
printf("nStudent %d detailsn",i);
scanf("%d",&s[i].rnum);
scanf("%s",s[i].name);
scanf("%d",&s[i].marks);
}
printf("nStudent Details are:");
printf("nRoll_numbertNametMarks");
for(i=1;i<=n;i++)
printf("n%dtt%st%dn",s[i].rnum,s[i].name,s[i].marks);
}
14
15
Parameter Passing in a
Function
 Structure variables can be passed as parameters like
any other variables. Only the values will be copied
during function invocation
16
#include<stdio.h>typedef
struct
{
int x;int y;
}POINT;
void display(int, int);void
main()
{
POINT p1 = {2, 3};
display(p1.x, p1.y);
}
void display(int a, int b)
{
printf(" The coordinates of the point are: %d %d", a, b);
}
17
#include <stdio.h>typedef
struct
{
int x;int y;
}POINT;
void main()
{
POINT p1 = {2, 3};
display(p1);
}
void display(POINT p)
{
printf("The coordinates of the point are: %d %d", p.x, p.y);
}
18
19
#include<stdio.h>
#include<string.h>
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
void main()
{
struct student stud1, *ptr_stud1;ptr_stud1 = &stud1;
ptr_stud1->r_no = 01;
strcpy(ptr_stud1->name, "Rahul");
strcpy(ptr_stud1->course, "BCA");
ptr_stud1->fees = 45000;
printf("n DETAILS OF STUDENT");
printf("n "); printf("n ROLL NUMBER = %d", ptr_stud1->r_no);
printf("n NAME = %s", ptr_stud1->name);
printf("n COURSE = %s", ptr_stud1->course);
printf("n FEES = %f", ptr_stud1->fees);
}
20
Defining data type: using typedef
 One may define a structure data-type with a single
name
typedef struct newtype {
member-variable1;
member-variable2;
.
member-variableN;
} mytype;
 mytype is the name of the new data-type
 Also called an alias for struct newtype
 Writing the tag name newtype is optional, can be
skipped
 Naming follows rules of variable naming
21
typedef : An example
typedef struct {
float real;
float imag;
} _COMPLEX;
 Defined a new data type named _COMPLEX. Now can
declare and use variables of this type
_COMPLEX a, b, c;
22
 Note: typedef is not restricted to just structures,
can define new types from any existing type
 Example:
 typedef int INTEGER
 Defines a new type named INTEGER from the
known type int
 Can now define variables of type INTEGER which
will have all properties of the int type
INTEGER a, b, c;
23
The earlier program using typedef
typedef struct{
float real;
float imag;
} _COMPLEX;
void swap (_COMPLEX a, _COMPLEX b)
{
_COMPLEX tmp;
tmp = a;
a = b;
b = tmp;
}
24
Contd.
void print (_COMPLEX a)
{
printf("(%f, %f) n",a.real,a.imag);
}
void main()
{
_COMPLEX x={4.0,5.0}, y={10.0,15.0};
print(x); print(y);
swap(x,y);
print(x); print(y);
}
swap.c
25
 Output:
(4.000000, 5.000000)
(10.000000, 15.000000)
(4.000000, 5.000000)
(10.000000, 15.000000)
 x and y are not swapped! But that has got
nothing to do with structures specially. We
will see its reason shortly
26
Exercise Problems
1. Extend the complex number program to include functions
for addition, subtraction, multiplication, and division
2. Define a structure for representing a point in two-
dimensional Cartesian co-ordinate system
• Write a function to compute the distance between two
given points
• Write a function to compute the middle point of the line
segment joining two given points
• Write a function to compute the area of a triangle, given
the co-ordinates of its three vertices

More Related Content

PPT
structures_v1.ppt
PPT
structures_v1.ppt
PPT
Structures and Unions in C-Language with Examples.ppt
PPT
09-structuresqwertyuiopasdfghjklmnbvcxzdfghjkl.ppt
PPT
Data structures Implementation 09-structures.ppt
PDF
C structure and union
PDF
Structures in c++
PDF
Structures in c++
structures_v1.ppt
structures_v1.ppt
Structures and Unions in C-Language with Examples.ppt
09-structuresqwertyuiopasdfghjklmnbvcxzdfghjkl.ppt
Data structures Implementation 09-structures.ppt
C structure and union
Structures in c++
Structures in c++

Similar to Introduction-to-structures-using-C-programming.ppt (20)

PDF
Chapter 13.1.9
PPTX
Structure & union
PDF
Lk module4 structures
PPTX
Chapter 2 part II array and structure.pptx
PPT
2 lesson 2 object oriented programming in c++
PDF
Unit 4 qba
PDF
Principals of Programming in CModule -5.pdf
PPT
L6 structure
PPTX
Programming for problem solving-II(UNIT-2).pptx
PPTX
data structure and c programing concepts
PPTX
detail structure presentation of problem solving
DOCX
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
PDF
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
DOC
9.structure & union
PDF
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
PPTX
Chapter4.pptx
PPTX
DS_Lecture_05.1_05.2.pptxnjnshsgxdhdgcdhcbdhc
PPTX
Structures
PDF
VIT351 Software Development VI Unit4
PPT
C Language_PPS_3110003_unit 8ClassPPT.ppt
Chapter 13.1.9
Structure & union
Lk module4 structures
Chapter 2 part II array and structure.pptx
2 lesson 2 object oriented programming in c++
Unit 4 qba
Principals of Programming in CModule -5.pdf
L6 structure
Programming for problem solving-II(UNIT-2).pptx
data structure and c programing concepts
detail structure presentation of problem solving
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
9.structure & union
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
Chapter4.pptx
DS_Lecture_05.1_05.2.pptxnjnshsgxdhdgcdhcbdhc
Structures
VIT351 Software Development VI Unit4
C Language_PPS_3110003_unit 8ClassPPT.ppt
Ad

Recently uploaded (20)

PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Digital Logic Computer Design lecture notes
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Geodesy 1.pptx...............................................
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Welding lecture in detail for understanding
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
additive manufacturing of ss316l using mig welding
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
R24 SURVEYING LAB MANUAL for civil enggi
Digital Logic Computer Design lecture notes
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Geodesy 1.pptx...............................................
Mechanical Engineering MATERIALS Selection
Welding lecture in detail for understanding
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
UNIT 4 Total Quality Management .pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
CYBER-CRIMES AND SECURITY A guide to understanding
additive manufacturing of ss316l using mig welding
bas. eng. economics group 4 presentation 1.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Ad

Introduction-to-structures-using-C-programming.ppt

  • 2. 2 What is a Structure?  Used for handling a group of logically related data items Examples:  Student name, roll number, and marks  Real part and complex part of a complex number  Helps in organizing complex data in a more meaningful way  The individual structure elements are called members
  • 3. 3 Defining a Structure struct tag { member 1; member 2; : member m; };  struct is the required C keyword  tag is the name of the structure  member 1, member 2, … are individual member declarations
  • 4. 4 Contd.  The individual members can be ordinary variables, pointers, arrays, or other structures (any data type)  The member names within a particular structure must be distinct from one another  A member name can be the same as the name of a variable defined outside of the structure  Once a structure has been defined, the individual structure-type variables can be declared as: struct tag var_1, var_2, …, var_n;
  • 5. 5 Example  A structure definition struct student { char name[30]; int roll_number; int total_marks; char dob[10]; };  Defining structure variables: struct student a1, a2, a3; A new data-type
  • 6. 6 A Compact Form  It is possible to combine the declaration of the structure with that of the structure variables: struct tag { member 1; member 2; : member m; } var_1, var_2,…, var_n;  Declares three variables of type struct tag  In this form, tag is optional
  • 8. 8 Structure Initialization  Structure variables may be initialized following similar rules of an array. The values are provided within the second braces separated by commas  An example: struct complex a={1.0,2.0}, b={-3.0,4.0}; a.real=1.0; a.imag=2.0; b.real=-3.0; b.imag=4.0;
  • 9. 9 Accessing a Structure  The members of a structure are processed individually, as separate entities  Each member is a separate variable  A structure member can be accessed by writing variable.member where variable refers to the name of a structure-type variable, and member refers to the name of a member within the structure  Examples: a1.name, a2.name, a1.roll_number, a3.dob
  • 10. 10 Example: Complex number addition void main() { struct complex { float real; float cmplex; } a, b, c; scanf (“%f %f”, &a.real, &a.cmplex); scanf (“%f %f”, &b.real, &b.cmplex); c.real = a.real + b.real; c.cmplex = a.cmplex + b.cmplex; printf (“n %f + %f j”, c.real, c.cmplex); }
  • 11. 11 Operations on Structure Variables  Unlike arrays, a structure variable can be directly assigned to another structure variable of the same type a1 = a2;  All the individual members get assigned  Two structure variables can not be compared for equality or inequality if (a1 == a2)…… this cannot be done
  • 12. 12 #include<stdio.h> struct employee { int emp_id; char name[10]; double salary; char designation [20]; float experience; }; void main() { struct employee e; printf(“%d”,sizeof(e)); }
  • 13. 13 : C program to read and display the student details using structures. #include<stdio.h> struct student { int rnum; char name[20];int marks; }s[60]; void main() { int i,n; printf("Enter the number of students"); scanf("%d",&n); printf("nEnter the roll number, Name , Marksn"); for(i=1;i<=n;i++) { printf("nStudent %d detailsn",i); scanf("%d",&s[i].rnum); scanf("%s",s[i].name); scanf("%d",&s[i].marks); } printf("nStudent Details are:"); printf("nRoll_numbertNametMarks"); for(i=1;i<=n;i++) printf("n%dtt%st%dn",s[i].rnum,s[i].name,s[i].marks); }
  • 14. 14
  • 15. 15 Parameter Passing in a Function  Structure variables can be passed as parameters like any other variables. Only the values will be copied during function invocation
  • 16. 16 #include<stdio.h>typedef struct { int x;int y; }POINT; void display(int, int);void main() { POINT p1 = {2, 3}; display(p1.x, p1.y); } void display(int a, int b) { printf(" The coordinates of the point are: %d %d", a, b); }
  • 17. 17 #include <stdio.h>typedef struct { int x;int y; }POINT; void main() { POINT p1 = {2, 3}; display(p1); } void display(POINT p) { printf("The coordinates of the point are: %d %d", p.x, p.y); }
  • 18. 18
  • 19. 19 #include<stdio.h> #include<string.h> struct student { int r_no; char name[20]; char course[20]; float fees; }; void main() { struct student stud1, *ptr_stud1;ptr_stud1 = &stud1; ptr_stud1->r_no = 01; strcpy(ptr_stud1->name, "Rahul"); strcpy(ptr_stud1->course, "BCA"); ptr_stud1->fees = 45000; printf("n DETAILS OF STUDENT"); printf("n "); printf("n ROLL NUMBER = %d", ptr_stud1->r_no); printf("n NAME = %s", ptr_stud1->name); printf("n COURSE = %s", ptr_stud1->course); printf("n FEES = %f", ptr_stud1->fees); }
  • 20. 20 Defining data type: using typedef  One may define a structure data-type with a single name typedef struct newtype { member-variable1; member-variable2; . member-variableN; } mytype;  mytype is the name of the new data-type  Also called an alias for struct newtype  Writing the tag name newtype is optional, can be skipped  Naming follows rules of variable naming
  • 21. 21 typedef : An example typedef struct { float real; float imag; } _COMPLEX;  Defined a new data type named _COMPLEX. Now can declare and use variables of this type _COMPLEX a, b, c;
  • 22. 22  Note: typedef is not restricted to just structures, can define new types from any existing type  Example:  typedef int INTEGER  Defines a new type named INTEGER from the known type int  Can now define variables of type INTEGER which will have all properties of the int type INTEGER a, b, c;
  • 23. 23 The earlier program using typedef typedef struct{ float real; float imag; } _COMPLEX; void swap (_COMPLEX a, _COMPLEX b) { _COMPLEX tmp; tmp = a; a = b; b = tmp; }
  • 24. 24 Contd. void print (_COMPLEX a) { printf("(%f, %f) n",a.real,a.imag); } void main() { _COMPLEX x={4.0,5.0}, y={10.0,15.0}; print(x); print(y); swap(x,y); print(x); print(y); } swap.c
  • 25. 25  Output: (4.000000, 5.000000) (10.000000, 15.000000) (4.000000, 5.000000) (10.000000, 15.000000)  x and y are not swapped! But that has got nothing to do with structures specially. We will see its reason shortly
  • 26. 26 Exercise Problems 1. Extend the complex number program to include functions for addition, subtraction, multiplication, and division 2. Define a structure for representing a point in two- dimensional Cartesian co-ordinate system • Write a function to compute the distance between two given points • Write a function to compute the middle point of the line segment joining two given points • Write a function to compute the area of a triangle, given the co-ordinates of its three vertices