Unit 4
Unit 4
Structure declaration-
struct tagname
………
………
};
OR
struct
98 *Under revision
Data type member3;
………
………
};
OR
struct tagname
struct element 1;
struct element 2;
struct element 3;
………
………
struct element n;
};
struct student
int age;
char name[20];
char branch[20];
99 *Under revision
}; struct student s;
Like primary variables structure variables can also be initialized when they are
declared. Structure templates can be defined locally or globally. If it is local it can
be used within that function. If it is global it can be used by all other functions of
the program.
struct student
int age=20;
char name[20]=”sona”;
}s1;
struct student
int age,roll;
char name[20];
If initialiser is less than no.of structure variable, automatically rest values are taken
as zero.
Dot operator is used to access the structure elements. Its associativety is from left
to right.
structure variable ;
s1.name[];
s1.roll;
s1.age;
Example:
#include<stdio.h>
#include<conio.h>
void main()
char branch;
} s1,s2;
s2.roll=s1.roll;
printf(“%d”, s2.roll);
Unary, relational, arithmetic, bitwise operators are not allowed within structure
variables.
Lecture Note:24
Size of structure-
Size of structure can be found out using sizeof() operator with structure variable
name or tag name with keyword.
sizeof(struct student); or
sizeof(s1);
sizeof(s2);
Array of structures
#include<stdio.h>
#include<string.h>
struct student