In this tutorial, we are going to learn about the basics of structures, their declarations, and their definitions.

Struct in C:

A Structure is a derived data type to organize a group of related data items of different data types referring to a single entity. i.e., a single variable capable of holding data items of different data types.

The data items in a structure are usually related like different kinds of information about a person or about a part or about an account, etc.

Each data item in a structure is called a member, sometimes these members are also called fields.

The keyword used to create a structure is struct.

The advantage of using a structure is that the accessibility of members becomes easier since all the members of a specific structure get the allocation of continuous memory and therefore it minimizes the memory access time.

Structures can be declared and defined in many ways.

Generally, a structure can be declared as:

struct tag_name {
       data_type_1 var_1;
       data_type_2 var_2;
       .....
       .....
       .....
       data_type_n var_n;
};

The declaration begins with the keyword struct. The list of declaration of its members must be enclosed in braces, the tag_name is an identifier that specifies the new structure name.

The declaration of a structure does not reserve any storage space. But the definition of the structure creates structure variables.

The structure variables can be defined as:

struct tag_name svar_1, svar_2, ......., svar_n;

Let’s consider an example:

struct example {
       int a;
       float b;
       char c;
};

struct example s1, s2, s3; // structure definition

In the above code example is a new user-defined data type that consists of 3 variables of different data types. The structure definition allocates a 7 bytes (2 + 4 + 1) memory to each structure variable s1, s2, and s3.

Understanding declarations and definitions of Structures:

Another way of declaring and defining a structure is:

struct {
      data_type1 var1;
      data_type2 var2;
      .....
      .....
      .....  
      data_typen varn;
}svar1, svar2;

In the above type of syntax, variables are declared directly with the declaration of a structure without choosing a name for the structure and only two structure variables can only be defined. So, It is not possible to create new variables of this structure type later in the program.

Another syntax is:

struct tag_name {
       data_type1 var1;
       data_type2 var2;
       .....
       .....
       .....
       data_typen varn;
}svar1, svar2;
.....
struct tag_name svar3, svar4;

Here, two structure variables are declared along with the structure tag, they can be used in the program where they are declared. A new structure of variables can also be defined by using tag_name and used in the program.

A structure can also be declared by using the keyword typedef as:

typedef struct tag_name {
        data_type1 var1;
        data_type2 var2;
        .....
        .....
        .....
        data_Typen varn;
} new_name;
new_name svar1, svar2, ....., svarn;

Here, typedef is used to create an alias name new_name for the user-defined structure struct tag_name, so new_name can be used to define structure variables of the given type.

Processing structure members with dot(.) operator:

Basically, there are two operators available for accessing the members of a structure depending on whether a normal variable or a pointer variable is declared to the structure.

They are:

  1. Dot (period) operator
  2. Arrow operator

To access a member of a structure using the dot (.) operator, the syntax is:

structure_variable.member

Let us consider an example

struct example {
       int a;
       float b;
       char c;
};
struct example e;
e.a = 25;
e.b = 45.67;
e.c = 'M';

In the above code e is a structure variable that has the memory of 3 variables ab and c. Each member can be accessed by using the operator dot (.) as e.a, e.b, and e.c.

structure variable can be initialized in its definition itself with a list of initializers enclosed within the braces. The initialization of values will be taken in order.

Let us consider another example:

#include <stdio.h>
void main() {
     struct book {
           char name[30];
           int pages;
           float price;
    };
    struct book b1 = {"C-Programming", 350, 500.00};
    struct book b2 = {"Play and Enjoy", 423, 754021};
    printf("First Book information is\nName : %s\nNumber of Pages: %d\nPrice of the book: %f\n", b1.name, b1.pages, b1.price);
    printf("Second Book information is\nName : %s\nNumber of Pages: %d\nPrice of the book: %f\n", b2.name, b2.pages, b2.price);
}

Output:

First Book information is
Name : C-Programming
Number of Pages: 350
Price of the book: 500.000000
Second Book information is
Name : Play and Enjoy
Number of Pages: 423
Price of the book: 754021.000000

REFERENCES:

Happy Learning 🙂