1 of 11

CSO101: Computer Programming

Structures & Unions

O.S.L. Bhavana

2 of 11

Structures

  • Structures/unions may also be declared without explicit name, in which case its variables can only be declared with definition.

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;}

  • No more variables of the above structures can be declared after the definition

3 of 11

Passing arrays as structure fields

  • When field of a structure is an array and the structure is being passed to a function then implicitly the array will also passed as a field of a structure
  • Ex: struct FY{int a[10]; char b;} x,y={1,2,3,4,5,6,7,8,9,0,'l’};

void func(struct FY q){ printf("%d",q.a[9]); }

int main( ){ func(y); return 0;} // prints 0

  • In the above example the structure and its fields are passed by value
  • Similarly one can write a variant with pass by reference

4 of 11

typedef

  • typedef is a keyword used to create synonyms for datatype names
  • Ex1: The statement “ typedef int Length; ” makes Length a

synonym for the data type int

    • Now variables of integer datatype may be declared in two ways
    • int a,b,c; //(or alternatively)
    • Length a,b,c;

  • Ex2: typedef struct student Stdnt; // lets to declare variables

of the structure student as

Stdnt a,b;

  • Ex3: typedef int * intptr; // lets declare integer pointer variables

as “ intptr ptr1, ptr2; ”

Here both ptr1 and ptr2 will be declared as integer pointers

5 of 11

typedef

  • Syntax: typedef Existing-typename New-typename;
  • Ex: typedef char* string; string a,b; // a,b are declared to be character pointers

6 of 11

typedef

  • We can also merge a struct definition and typedef statement
  • In this example cubd is defined as short form for struct cuboid

7 of 11

typedef

  • typedef helps in writing a more readable code
  • typedef too has scope restrictions

8 of 11

Unions

  • Union is a user-defined data type in C
  • Like Structures, unions also have fields/members of multiple datatypes .
  • Unions allow to store variables of different datatypes at same memory location
  • Unions are cleverly used to save memory
  • Unlike in structures, all the fields of a union share the memory. Recall, for structures each field has a separate allocation in memory

9 of 11

Unions

  • Memory allocated to a union is the amount of memory required to store its largest field. Ex: sizeof(xyz) = 4
  • Total 4 bytes of memory is allocated to an xyz variable

10 of 11

Unions

  • union is a keyword used to define unions
  • Memory allocated by union is shared by the fields of the union
  • Modifying value of any field will also effect the values of other fields
  • In list initialization, only the first field of the union is initialized.

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

  • Access to an field
    • results in correct value if it is the last field to be updated
    • results in incorrect/garbage values if it is not the last updated field
  • Fields of unions can be accessed using ‘.’ and -> operators appropriately
  • Direct assignment is also possible between two unions of same type

11 of 11

Use of Pointers and Functions with Unions