Structure and Union in C

Structure and Union in C

Structure and union in c

Structure and Union:-

Structure:-

Structure is a mechanism which groups different data member to form a record.
A structure is suitable for creating a database memory link and link list and creating bit-field.
Basically, structure is used to defines as a record.

Syntax:-
struct [structure name]
{
member definition;
member definition;
...
member definition;
};

Accessing the Structure Member

To access any member of a structure, we use the member access operator (.) .

Suppose you want to know about a student’s details. You might want to know the following attribute of the student.

roll
name
age
branch

PROGRAM:-
#include"stdio.h"
struct student
{
int roll;
char name[20];
int age;
char branch[30];
};
main()
{
struct student m={1,"s.das",19,"BSC"};
printf("%d %s %d %s",m.roll,m.name,m.age,m.branch);
}

O/P:-
1 s.das 19 BSC

Nested Structure:-

A structure nested with another structure is called nested structure.

PROGRAM:-
struct XXX
{
int a; 
int b;
};
struct YYY
{
int c;
int d; //Here structure XXX nested with YYY structure.
struct XXX p;
};
main()
{
struct YYY q;
}

Self-referential structure:-

When a structure nested with the same structure and the nested structure variable is a pointer that is called a self-referential structure.
A self-referential structure is used for memory leak and link list.

struct XXX
{
int a;
int b; //This is a selfreferential structure.
struct XXX *p;
};
main()
{

}

Union:-

Union is a mechanism which permits to share a common memory. It is used for locking mechanism.

Syntax:-
union [union name]
{
member definition;
member definition;
...
member definition;
};

Accessing the Union Member

To access any member of a Union, we use the member access operator (.) .

PROGRAM:-
#include <stdio.h>
union Data
{
int a;
float b;
char st[20];
};
int main( ) {
union Data data;
data.a = 20;
data.b = 200.1;
strcpy( data.st, "Union");

printf( "data.a : %d\n", data.a);
printf( "data.b : %f\n", data.b);
printf( "data.st : %s\n", data.st);

return 0;
}

 

PROGRAM:-
#include <stdio.h>
#include <string.h>

union Data
{
int i;
float f;
char str[40];
};
int main( ) {
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
PROGRAM:-
Memory size occupied by data :40

In the above example, Data type will occupy 40 bytes of memory space because this is the maximum space which can be occupied by a character string

The very first data member of union is called active data member. The active data member of the union must be the largest data member.

IMPORTANT POINTS ON STRUCTURE AND UNION:-

1. If a structure contains a slack byte, that is called imbalance structure

2. Word alignment is required to make an imbalanced structure to balance structure

3. The same structure cannot be nested within the same structure, because it is a restriction in C

4. Same structure can be nested within same structure if nested structure is a pointer , that is called self-referential structure

5. Structure is used for data encapsulation, memory link, and bit-fields

6. Two structure variable cannot be compared because it is the restriction in C

7. One structure variable can be assigned to another structure variable if both belong to the same structure

8. Offset of command is used to measure the byte offset of a structure

9. Bit field is a unique feature in C which can be applied to a structure for memory optimization

10. Bit field range must be 1 to size of data member

11. Bit field is not allowed to float data members

12. The very first member of the union is active member

13. First member of the union only can be initialized at the time of definition and rest can’t be initialized

14. Union provides a technique to share a common memory in different ways

15. The longest data member size is the union size

16. Properties of union depends on the type of first data member

17. Active data member must be the longest data member, otherwise, message can’t pass to the other data member

18. Union is used for memory optimization, Creating message format and share memory or locking mechanism

19. Each Data member of a union begins at same place, But each data member of a structure begins at different places

20. Byte offset of the union data member is always 0.

21. To create a memory link or linked list we need a structure and that structure must be a self-referential structure

22. Data member can be accessed using. notation or -> notation

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community