A structure is a convenient tool for handling a group of logically related data items. Structure help to organize complex data is a more meaningful way. It is powerful concept that we may after need to use in our program Design.

A structure is combination of different data types. Lets take the example of a book, if we cant to declare a book we will be thinking about the name, title, authors and publisher of the book and publishing year. So to declare a book we need to have some complex data type which can deal with more than one data types.

Table of Contents

So, how we can define and declare and initialize a structure in a C Program?

How to Define a Structure in C?

We can define a structure by using the keyword struct followed by list of member variables contained in curly braces

The keyword struct defines a book, and each line with in the braces defines the elements of the Book. Now when ever we create an instance of Book it will have all the elements of the structure i.e. Name, Author, Publisher and Year.

How to Declare a Structure in C?

We can declare the structure just like union by using the following syntax.

How to Initialize a Structure in C?

Lets initialize the structure variable CProgrammingBook.

Accessing the Members of a Structure

We can access all the members of a structure by adding a period after the name of the structure variable name and then the name of the field we want to access. For example we want to change the Publish year from 2001 to 2002, we will do it as CProgrammingBook.Year = 2002;

Array of Structures

Each element of the array itself is a structure see the following example shown below. Here we want to store data of 5 persons for this purpose, we would be required to use 5 different structure variables, from sample1 to sample 5. To have 5 separate variable will be inconvenient.

The structure type person is having 2 elements: Name an array of 25 characters and integer type variable age. Using the statement struct person sample[5]; we are declaring a 5 element array of structures. Here, each element of sample is a separate structure of type person. We, then defined 2 variables into index and an array of 8 characters, info. Here, the first loop executes 5 times, with the value of index varying from 0 to 4. The first printf()  function displays. For the first time this name you enter will go to sample[0]. name.  The second for loop in responsible for printing the information stored in the array of structures.

Nesting Structures in C

Structure with in a structure means nesting of structures. Let us consider the following structure defined to store information about the salary of employees.

This structure defines name, department, basic pay and 3 kinds of allowance. we can group all the items related to allowance together and declare them under a substructure are shown below:

The salary structure contains a member named allowance which itself is a structure with 3 members. The members contained in the inner, structure namely dearness, house_rent, and city can be referred to as :

An inner-most member in a nested structure can be accessed by chaining all the concerned. Structure variables (from outer-most to inner-most) with the member using dot operator. The following being invalid.

Structures are passed to functions by way of their pointers. Thus, the changes made to the structure members inside the function will be reflected even outside the function.

Output of the C Program:

How to Compare Two Structures in C?

You can use memcmp() function. If your structure has pointers, then move them to the end and do not compare memory area for the pointer. You have to compare pointers explicitly.

See sample program below.

The memcmp() method needs a bit of extra caution because of memory alignment considerations. For example:

The memory layout of the struct A will be, on a 32 bit system with 4 byte alignment:

4 bytes for n;
1 byte for c;
3 unused bytes (garbage);
4 bytes for m;

If you use memcmp(), it can be that the 4 + 1 + 4 used bytes are identical but that there is a difference between the 3 garbage bytes. I don’t think in this case you want your comparison function to return false.

A workaround can be something like:

If you have pointers in your structures, I suggest that you use a different structure for your “raw” data and add the pointers later:

Structure and Union specifiers have the same form. [ . . . ] The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa.

— ANSI/ISO 9899:1990 (the ANSI C standard) Section 6.5.2.1