SlideShare una empresa de Scribd logo
1 de 22
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
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
8
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);
}
9
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
10
Arrays of Structures
 Once a structure has been defined, we can
declare an array of structures
struct student class[50];
The individual members can be accessed as:
class[i].name
class[5].roll_number
type name
11
Arrays within Structures
 A structure member can be an array
 The array element within the structure can
be accessed as:
a1.marks[2], a1.dob[3],…
struct student
{
char name[30];
int roll_number;
int marks[5];
char dob[10];
} a1, a2, a3;
12
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;
13
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
void swap (struct complex a, struct complex b)
{
struct complex tmp;
tmp=a;
a=b;
b=tmp;
}
14
Returning structures
 It is also possible to return structure values from a
function. The return data type of the function should
be as same as the data type of the structure itself
struct complex add(struct complex a, struct complex b)
{
struct complex tmp;
tmp.real = a.real + b.real;
tmp.imag = a.imag + b.imag;
return(tmp);
}
Direct arithmetic operations are not possible with structure variables
15
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
16
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;
17
 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;
18
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;
}
19
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
20
 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
Union
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %dn", data.i);
printf( "data.f : %fn", data.f);
printf( "data.str : %sn", data.str);
return 0;
}
21
data.i : 1917853763
data.f:122360580327794860452759994368.00000
data.str : C Programming
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
printf( "data.i : %dn", data.i);
data.f = 220.5;
printf( "data.f : %fn", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %sn", data.str);
return 0;
} 22
data.i : 10
data.f : 220.500000
data.str : C Programming
Union

Más contenido relacionado

Similar a structures_v1.ppt

Similar a structures_v1.ppt (20)

2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdf
 
#Jai c presentation
#Jai c presentation#Jai c presentation
#Jai c presentation
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
Chap 10(structure and unions)
Chap 10(structure and unions)Chap 10(structure and unions)
Chap 10(structure and unions)
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structure.pptx
Structure.pptxStructure.pptx
Structure.pptx
 
Structures
StructuresStructures
Structures
 
Str
StrStr
Str
 
Unit 1_ADC.pptx
Unit 1_ADC.pptxUnit 1_ADC.pptx
Unit 1_ADC.pptx
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 

Último

Presentation for the country presentation
Presentation for the country presentationPresentation for the country presentation
Presentation for the country presentationjalal879
 
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...amitlee9823
 
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制yynod
 
Miletti Gabriela_Vision Plan for artist Jahzel.pdf
Miletti Gabriela_Vision Plan for artist Jahzel.pdfMiletti Gabriela_Vision Plan for artist Jahzel.pdf
Miletti Gabriela_Vision Plan for artist Jahzel.pdfGabrielaMiletti
 
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...rightmanforbloodline
 
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfreStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfKen Fuller
 
➥🔝 7737669865 🔝▻ Mirzapur Call-girls in Women Seeking Men 🔝Mirzapur🔝 Escor...
➥🔝 7737669865 🔝▻ Mirzapur Call-girls in Women Seeking Men  🔝Mirzapur🔝   Escor...➥🔝 7737669865 🔝▻ Mirzapur Call-girls in Women Seeking Men  🔝Mirzapur🔝   Escor...
➥🔝 7737669865 🔝▻ Mirzapur Call-girls in Women Seeking Men 🔝Mirzapur🔝 Escor...amitlee9823
 
Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.GabrielaMiletti
 
Personal Brand Exploration ppt.- Ronnie Jones
Personal Brand  Exploration ppt.- Ronnie JonesPersonal Brand  Exploration ppt.- Ronnie Jones
Personal Brand Exploration ppt.- Ronnie Jonesjonesyde302
 
Nagavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore Es...
Nagavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore Es...Nagavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore Es...
Nagavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore Es...amitlee9823
 
➥🔝 7737669865 🔝▻ bharuch Call-girls in Women Seeking Men 🔝bharuch🔝 Escorts...
➥🔝 7737669865 🔝▻ bharuch Call-girls in Women Seeking Men  🔝bharuch🔝   Escorts...➥🔝 7737669865 🔝▻ bharuch Call-girls in Women Seeking Men  🔝bharuch🔝   Escorts...
➥🔝 7737669865 🔝▻ bharuch Call-girls in Women Seeking Men 🔝bharuch🔝 Escorts...amitlee9823
 
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...sonalitrivedi431
 
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...poojakaurpk09
 
Chintamani Call Girls Service: ☎ 7737669865 ☎ High Profile Model Escorts | Ba...
Chintamani Call Girls Service: ☎ 7737669865 ☎ High Profile Model Escorts | Ba...Chintamani Call Girls Service: ☎ 7737669865 ☎ High Profile Model Escorts | Ba...
Chintamani Call Girls Service: ☎ 7737669865 ☎ High Profile Model Escorts | Ba...amitlee9823
 
Gabriel_Carter_EXPOLRATIONpp.pptx........
Gabriel_Carter_EXPOLRATIONpp.pptx........Gabriel_Carter_EXPOLRATIONpp.pptx........
Gabriel_Carter_EXPOLRATIONpp.pptx........deejay178
 
➥🔝 7737669865 🔝▻ Pallavaram Call-girls in Women Seeking Men 🔝Pallavaram🔝 E...
➥🔝 7737669865 🔝▻ Pallavaram Call-girls in Women Seeking Men  🔝Pallavaram🔝   E...➥🔝 7737669865 🔝▻ Pallavaram Call-girls in Women Seeking Men  🔝Pallavaram🔝   E...
➥🔝 7737669865 🔝▻ Pallavaram Call-girls in Women Seeking Men 🔝Pallavaram🔝 E...amitlee9823
 
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...amitlee9823
 

Último (20)

Presentation for the country presentation
Presentation for the country presentationPresentation for the country presentation
Presentation for the country presentation
 
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
 
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
 
Miletti Gabriela_Vision Plan for artist Jahzel.pdf
Miletti Gabriela_Vision Plan for artist Jahzel.pdfMiletti Gabriela_Vision Plan for artist Jahzel.pdf
Miletti Gabriela_Vision Plan for artist Jahzel.pdf
 
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
 
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfreStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
 
➥🔝 7737669865 🔝▻ Mirzapur Call-girls in Women Seeking Men 🔝Mirzapur🔝 Escor...
➥🔝 7737669865 🔝▻ Mirzapur Call-girls in Women Seeking Men  🔝Mirzapur🔝   Escor...➥🔝 7737669865 🔝▻ Mirzapur Call-girls in Women Seeking Men  🔝Mirzapur🔝   Escor...
➥🔝 7737669865 🔝▻ Mirzapur Call-girls in Women Seeking Men 🔝Mirzapur🔝 Escor...
 
Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.
 
Personal Brand Exploration ppt.- Ronnie Jones
Personal Brand  Exploration ppt.- Ronnie JonesPersonal Brand  Exploration ppt.- Ronnie Jones
Personal Brand Exploration ppt.- Ronnie Jones
 
Nagavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore Es...
Nagavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore Es...Nagavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore Es...
Nagavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore Es...
 
➥🔝 7737669865 🔝▻ bharuch Call-girls in Women Seeking Men 🔝bharuch🔝 Escorts...
➥🔝 7737669865 🔝▻ bharuch Call-girls in Women Seeking Men  🔝bharuch🔝   Escorts...➥🔝 7737669865 🔝▻ bharuch Call-girls in Women Seeking Men  🔝bharuch🔝   Escorts...
➥🔝 7737669865 🔝▻ bharuch Call-girls in Women Seeking Men 🔝bharuch🔝 Escorts...
 
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...
 
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
 
Chintamani Call Girls Service: ☎ 7737669865 ☎ High Profile Model Escorts | Ba...
Chintamani Call Girls Service: ☎ 7737669865 ☎ High Profile Model Escorts | Ba...Chintamani Call Girls Service: ☎ 7737669865 ☎ High Profile Model Escorts | Ba...
Chintamani Call Girls Service: ☎ 7737669865 ☎ High Profile Model Escorts | Ba...
 
Gabriel_Carter_EXPOLRATIONpp.pptx........
Gabriel_Carter_EXPOLRATIONpp.pptx........Gabriel_Carter_EXPOLRATIONpp.pptx........
Gabriel_Carter_EXPOLRATIONpp.pptx........
 
➥🔝 7737669865 🔝▻ Pallavaram Call-girls in Women Seeking Men 🔝Pallavaram🔝 E...
➥🔝 7737669865 🔝▻ Pallavaram Call-girls in Women Seeking Men  🔝Pallavaram🔝   E...➥🔝 7737669865 🔝▻ Pallavaram Call-girls in Women Seeking Men  🔝Pallavaram🔝   E...
➥🔝 7737669865 🔝▻ Pallavaram Call-girls in Women Seeking Men 🔝Pallavaram🔝 E...
 
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
 

structures_v1.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
  • 7. 7 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
  • 8. 8 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); }
  • 9. 9 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
  • 10. 10 Arrays of Structures  Once a structure has been defined, we can declare an array of structures struct student class[50]; The individual members can be accessed as: class[i].name class[5].roll_number type name
  • 11. 11 Arrays within Structures  A structure member can be an array  The array element within the structure can be accessed as: a1.marks[2], a1.dob[3],… struct student { char name[30]; int roll_number; int marks[5]; char dob[10]; } a1, a2, a3;
  • 12. 12 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;
  • 13. 13 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 void swap (struct complex a, struct complex b) { struct complex tmp; tmp=a; a=b; b=tmp; }
  • 14. 14 Returning structures  It is also possible to return structure values from a function. The return data type of the function should be as same as the data type of the structure itself struct complex add(struct complex a, struct complex b) { struct complex tmp; tmp.real = a.real + b.real; tmp.imag = a.imag + b.imag; return(tmp); } Direct arithmetic operations are not possible with structure variables
  • 15. 15 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
  • 16. 16 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;
  • 17. 17  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;
  • 18. 18 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; }
  • 19. 19 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
  • 20. 20  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
  • 21. Union union Data { int i; float f; char str[20]; }; int main( ) { union Data data; data.i = 10; data.f = 220.5; strcpy( data.str, "C Programming"); printf( "data.i : %dn", data.i); printf( "data.f : %fn", data.f); printf( "data.str : %sn", data.str); return 0; } 21 data.i : 1917853763 data.f:122360580327794860452759994368.00000 data.str : C Programming
  • 22. union Data { int i; float f; char str[20]; }; int main( ) { union Data data; data.i = 10; printf( "data.i : %dn", data.i); data.f = 220.5; printf( "data.f : %fn", data.f); strcpy( data.str, "C Programming"); printf( "data.str : %sn", data.str); return 0; } 22 data.i : 10 data.f : 220.500000 data.str : C Programming Union