CSO101: Computer Programming
Structures & Unions
O.S.L. Bhavana
Structures
struct {int a; char b;} x,y;
union {int a; char b;} u,v;
int main(){scanf("%d", &u.a); printf("Hello World %d", u.a); return 0;}
Passing arrays as structure fields
void func(struct FY q){ printf("%d",q.a[9]); }
int main( ){ func(y); return 0;} // prints 0
typedef
synonym for the data type int
of the structure student as
Stdnt a,b;
as “ intptr ptr1, ptr2; ”
Here both ptr1 and ptr2 will be declared as integer pointers
typedef
typedef
typedef
Unions
Unions
Unions
Ex: union xyz var1 = {‘A’,23,4.5} is valid. union xyz var1 = {2.3} is invalid
The type of the first member must match with the first value in list initialization
Use of Pointers and Functions with Unions