Structures C Tutorial

Bodhak structures c tutorial in this chapter we will discuss in details about Introduction to Structures, Advantages of using Structures, Syntax of Structure, Init Structure members, Structures with arrays, Manipulating structures using functions, Working with bit fields, Nested structures.

Introduction to Structures: A structure is a collection of one or more variables, which may be of different types, grouped under a single name.


structures c tutorial

For example, if we consider the student details, we may have to refer students register no, his name, his marks in subjects, and average. Such data are of different types, but they give meaningful information when grouped together.

In such environments, the data type ‘struct’ (structure) in C is highly helpful. A group of one or more variables of different types organized together under a single variable name is called as a structure.

C structure allows you to wrap one or more variables with different data types into one. A structure can contain any valid data types like int, char, float, array or even other structures.

Two operators are used to access members of structures: The structure member operator (.) also called the dot operator and the structure pointer operator (*) also called the arrow operator. The structure member operator accesses a structure member via the structure variable name.

<structure variable>. <member>;

struct student


int rno;

int age;

char sex;

float height;

float weight;


s1, s2;  s1.rno, s1.age, s1.height etc

The C compiler will not read or write an entire structure as a single command.  It will read or write the members of a structure as follows:

scanf(“%d”, &s1.rno);





Structures with arrays:

Just as arrays of basic types such as integers and floats are allowed in C, so are arrays of structures. An array of structures is declared in the usual way:

struct personal_data my_struct_array[100];

The members of the structures in the array are then accessed by statements such as the following:

The value of a member of a structure in an array can be assigned to another variable, or the value of a variable can be assigned to a member. For example, the following code assigns the number 1974 to the year_of_birth member of the fourth element of my_struct_array:

my_struct_array[3].year_of_birth = 1974;

(Like all other arrays in C, struct arrays start their numbering at zero.)

The following code assigns the value of the year_of_birth member of the fourth element of my_struct_array to the variable yob:

yob = my_struct_array[3].year_of_birth

Finally, the following example assigns the values of all the members of the second element of my_struct_array, namely my_struct_array[1], to the third element, so my_struct_array[2] takes the overall value of my_struct_array[1].

my_struct_array[2] = my_struct_array[1];

Next Page