Structure & Unions In C - Online Article

Array can be used to represent a group of data items that are of same data type such as integer, character, float etc. But if we want to store different types of data items then we can not use an array. 'C' supports a constructed data type called 'structure'. Structure is users define data type.

Structure can be use to store logically related data items like if we want to store the information of student such as student name his\her branch, roll number than we can not store it in a single data structure. But using structure we can create data type having three fields a character array (string) to store the name of student, an integer type variable to store the roll number, and another string to store the branch.

struct student
{
	char name[15];
	char branch[5];
	int r_number;
};
struct student s1;

In the above example we create a structure of type book which have three members, two are character string and one is integer. Each structure is called node. Here s1 is a variable of student type structure.

Now we can also make the array of student type variable by using the following command-

struct student s[9];

This will create an array of data type student with 9 continuous student type objects. We can define structure with another way:

struct student
{
	char name[15];
	char branch[5];
	int r_number;
}s1;

This will work same like the above declaration. We can use the members variable of the structure by dot (.) operator. If we want to use the name of the student then we can retrieve with the following way,

s1.name

Suppose we want to print all the above information of the student then we use printf function like this,

printf("Name of the student is = %s",s1.name);
printf("Branch of the student is = %s",s1.branch);
printf("Roll number of the student is = %d",s1.r_number);

Initialization of structure

Same like the initialization of array we can initialize the structure main different is that we have use the argument of the data type same as the data type of the members.

Example

main()
{
	struct s_record
	{
		int weight;
		float height;
	};
	static struct s_record s1={60,180.5};
	static struct s_record s2={55,170.75};
	printf("\nWeight of first student=%d",s1.weight);
	printf("\nHeight of first student=%f",s1.height);
	printf("\nWeight of second student=%d",s2.weight);
	printf("\nHeight of second student=%d",s2.height);
}

Answer

Weight of first student=60
Height of first student=180.5
Weight of second student=55 Height of second student=170.75

Making a data type using structure

We can create our own data type by using the keyword "typedef". In the above example there is a need of using struct keyword to define the variable but when we use typedef keyword there is no need of struct keyword. When we want to define own data type we create it out side the main function by using the typedef.

Example

typedef struct
{
	char name[15];
	char branch[5];
	int r_number;
}student;
main()
{
	student s1={"Smith","CSE",108};
	printf("Name of the student is = %s",s1.name);
	printf("Branch of the student is = %s",s1.branch);
	printf("Roll number of the student is = %d",s1.r_number);
}

Answer

Name of the student is = Smith
Branch of the student is = CSE Roll number of the student is = 108

Here we create a data type having three fields name, branch, and roll number and create a variable of data type "student" s1. After this we initialize it and then use it with the help of dot (.) operator.

Structures within structure

We can also creates the structures within the structure. This is same as the nested statement.

Example

struct emp
{
	char name[15];
	struct
	{
		int da;
		int ca;
		int hra;
		int p;
	}
	allowance;
}e1;

Answer

If we want to access name than we can access it with the dot (.) operator

e1.name;

But if we want to access the da then we can access it like this,

e1.allowance.da;

By the same way we can access each nested member of the structure. It is also possible that nested structure again have a structure within it than we will access that structures members similarly.

UNION

In structure each member has its own location where as all the members of the union uses the same location and take memory location according to the largest data type use in the union. It implies that although a union may contain many members of different, it can handle only one member at a time.

Memory allocation for the example given blow,

Example

main()
{
	union item
	{
		int x;
		char c;
		float f;
	}code;
	code.x=22;
	code.f=48.6;
	printf("The size of the code is = %d", sizeof(code));
	printf("\nThe value of x is=%d",code.x);
} 

Answer

The size of the code is =4
The value of x is=any value according to the value of float variable

Explanation:Here the size of the code will be 4 byte because float is the largest data type with 4 bytes. First we assign the 22 to the integer type variable x, then again assign 48.6 to the float type variable f, it is a union type structure hence 48.6 will be over write and when we retrieve the x the equivalent integer value of first two bytes will be displayed. If we want to display c then the character equivalent value of first byte will be displayed.

About the Author:

No further information.




Comments

No comment yet. Be the first to post a comment.