Structure in C. Introduction: A structure in C is a user defined data type that contains of a collection of related data times, which can be of different types grouped under a single name. The data items enclosed within a structure are known as its members which can be either be of int, float, char etc. data type . As a structure may contain items of different types so it can be viewed as collection of heterogenous data items.

The variable of structure type can be created through which we can manipulate its members. This property help us to organize complex data items efficiently in a program.



Structure Declaration : A structure declaration specifies the grouping of variables of different types in a single unit. It simply acts as a template or blueprint that may be used to create structure variables.

The syntax for declaring a structure in C is



struct struct_name

{

data_type var_name1;

data_type var_name2;

.............................

data_type var_namen;

};



The declaration starts with the structure header that consist of keyword struct followed by a tag serves as the structure name. It can be used for creating structure variables. This tag is optional but if the structure declaration and definition are at different places then it is necessary. The individual members of the structure are declared and enclosed within curly braces. The semicolon placed at the end of closing brace marks the end of structure declaration. Members in a structure are defined by the data type followed by an identifier . Declaration involving different data types must be separated by semicolons.



Declaration of structure variables : The declaration of structure variable is similar to the declaration of variable of fundamental data type, e.g., int, char, float, double. It consist of the keyword struct, the structure tag name, if series of variables present then to separate it by giving comma in each variable and at the end of the statement semicolon is used for termination.



The syntax of declaring structure variable is:

struct struct_name variable1, variable2, ------, variable_n;



Example :



struct student s1,s2,s3;

struct student

{

char name[10];

int roll_no;

char branch[10];

};

struct student s1,s2;



Accessing structure member: In array concept the individual/each element of the array is accessed using subscript. But in structure concept it does not follow the subscript notation. To access the members of a structure the dot(.) is used which is called dot operator or period operator or member operator. This operator makes or establishes a connection between members of the structure with the structure variables. Let's see the example below as ;

s1.name;

Here s1 is the structure variable and name is the structure member. So the dot operator maintains the relationship between s1 and name to access the name which is initialized.

Example for accessing structure members:

#include<stdio.h>

void main()

{

struct student

{

char name[30];

int roll_no;

char branch[30];

};

struct student s1;

printf("Input the name , roll. no. , branch:");

scanf("%s %d %s" , s1.name,&s1.roll_no, s1.branch);

printf("The name , roll. no. , branch is : %s %d %s", s1.name , &s1.roll_no, s1.branch);

getch();

}



OUTPUT:

Input the name , roll. no. , branch: Dhabal 15 BCA

The inputted name, roll.no. , branch is : Dhabal 15 BCA



Structure variable initialization: Whenever a programmer assign the values to structure variable it is called structure variable initialization.

Example of structure variable initialization is :

#include<stdio.h>

void main()

{

struct

{

char name[30];

int roll_no;

char branch[30];

}

s1={"Ram", 100, "civil");

printf("Name\t Roll. no.\t Branch

");

printf("%s\t %d\t %s" , s1.name, s1.roll_no,s1,branch);

getch();

}



OUTPUT:

Name Roll. no. Branch

Ram 100 Civil



Nested structure: Structure within structures is called nested structure , i.e., one structure can be declared/defined inside other structure.



Array of structure: In structure if we want to store 20 student names then we would required to create 10 structure variables individually from s1 to s20 which is not efficient. A better solution is to use the concept array of structure. An array of structure means it is an array of similar data types which themselves have a collection of dissimilar data types. In an array of structure all the elements of the array are stored in adjacent memory locations. Since each element of an array is a structure type, so all structure elements are stored in adjacent location, e.g. , s[0].name , s[0].roll_no , s[0].branch in memory followed by s[1].name , s[1].roll_no , s[1].branch and so on.



Pointer to structure : Pointer to structure means a pointer variable pointing to a structure variable instead of general declaration of variable. Such pointer variable is called structure pointer. We know that the pointer is a variable which holds the address of another variable , i.e., 1 pointer pointing to int, char, float or double.







THANKS