Unit-4: Structure

What is a structure?
A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.

 
How to create a structure?
‘struct’ keyword is used to create a structure. Following is an example. structaddress{ char name[50]; char street[100]; char city[50]; char state[20]; int pin;};

 
How to declare structure variables?
A structure variable can either be declared with structure declaration or as a separate declaration like basic types. // A variable declaration with structure declaration. struct Point { intx, y;} p1;  // The variable p1 is declared with'Point'. // A variable declaration like basic data types structPoint{intx, y;};.  intmain(). {structPoint p1;  // The variable p1 is declared like a normal variable}

How to initialize structure members?
Structure members cannot be initialized with declaration. For example the following C program fails in compilation. structPoint. {intx = 0;  // COMPILER ERROR:  cannot initialize members here inty = 0;  // COMPILER ERROR:  cannot initialize members here}; 

The reason for above error is simple, when a datatype is declared, no memory is allocated for it. Memory is allocated only when variables are created.

Structure members can be initialized using curly braces ‘{}’. For example, following is a valid initialization.

structPoint{intx, y;};  intmain() { // A valid initialization. member x gets value 0 and y. // gets value 1.  The order of declaration is followed.

structPoint p1 = {0, 1}; }

How to access structure elements?
Structure members are accessed using dot (.) operator.

#include<stdio.h>

struct Point

{

intx, y;};

int main()

{struct Point p1 = {0, 1};// Accessing members of point p1

p1.x = 20;

printf("x = %d, y = %d", p1.x, p1.y);

return0;}

Output:x = 20, y = 1

What is a structure pointer?
Like primitive types, we can have pointer to a structure. If we have a pointer to structure, members are accessed using arrow ( -> ) operator.

#include<stdio.h>

struct Point{intx, y;};

int main()

{struct Point p1 = {1, 2};// p2 is a pointer to structure p1

struct Point *p2 = &p1;// Accessing structure members using structure pointer

printf("%d %d", p2->x, p2->y);

return0;

}

Output:1 2

Nested Structures in C

A structure can be nested inside another structure. In other words, the members of a structure can be of any other type including structure. Here is the syntax to create nested structures.

Syntax:

structure tagname_1

{    member1;   

member2;    

member3;    …  

membern;     

structure tagname_2    

{        member_1;        

member_2;      

member_3;     

…        member_n;   

}, var1 

} var2;

Note: Nesting of structures can be extended to any level.

To access the members of the inner structure, we write a variable name of the outer structure, followed by a dot(.) operator, followed by the variable of the inner structure, followed by a dot(.) operator, which is then followed by the name of the member we want to access.

var2.var1.member_1 – refers to the member_1 of structure tagname_2
var2.var1.member_2 – refers to the member_2 of structure tagname_2

Union in C

Like Structure union is a user defined data type. In union, all members share the same memory location.

For example in the following C program, both x and y share the same location. If we change x, we can see the changes being reflected in y.

#include <stdio.h>

// Declaration of union is same as structures

union test

{ intx, y; };

int main() {

// A union variable t

union test t;

t.x = 2;

// t.y also gets value 2

printf("After making x = 2:\n x = %d, y = %d\n\n", t.x, t.y);

t.y = 10; // t.x is also updated to 10

printf("After making y = 10:\n x = %d, y = %d\n\n", t.x, t.y);

return0;

}

Output:After making x = 2:

x = 2, y = 2

After making y = 10:

x = 10, y = 10

Pointers to unions
Like structures, we can have pointers to unions and can access members using the arrow operator (->). The following example demonstrates the same.

#include <stdio.h>

union test

{ intx; chary; };

int main()

{

union test p1;

p1.x = 65; // p2 is a pointer to union p1

union test* p2 = &p1; // Accessing union members using pointer

printf("%d %c", p2->x, p2->y);

return0;

}

Output:65 A

Difference between STRUCTURES UNION

Struct keyword is used to declare the structure Union keyword is used to declare the Union

Structure variable will allocate memory for all the structure members separately. Union variable will allocate common memory for all the union members.

Example:struct Employee{ int age; char name[50]; float salary; };

Example:union Employee{ int age; char name[50]; float salary; };

Structures will occupy more memory space.Memory_Size = addition of all the structure members sizes.
Memory_Size = int + char array [50] + float
Memory_Size = 2 + 50 + 4 Bytes
Memory_Size = 56 Byte

Union will occupy less memory space compared to structures.Memory_Size = Size of the largest Union member. From the above example, the Largest Union member is char array. So, Memory_Size = 50 Bytes

Structure allows us to access any or all the members at any time.

Union allows us to access only one union member at a time.