C Programming

How to Make an Array of Structs in C

We will first understand the structures in C and then we will discuss about the array of structures in C. We will also go through the array of structure pointers and how to access the structure members within the array of structure objects.

Structs are the user defined group of similar or different data types. Similar or different group of data types can be grouped under one name known as structures. Example declaration of structure in C:

Struct abc {

Int a;

Char b;

Float c;

};

To create object from the structure declaration, here is the syntax in C to do so:

Struct abc obj;

With this object creation, memory is assigned to the structure. To access the member of structures with the help of object(obj) can be done as:

Obj.a = 10; obj.b = ‘c’; obj.c = 1.0;

This is the case when object is created, there is no need to allocate any memory. Once the object is instantiated memory will be allocated automatically.

Allocation of memory to the structure can be done at runtime. For runtime allocation, malloc function can be used.

There will be a declaration of pointer to the structure. Declaration of the pointer to the structure can be done as struct abc *ptr; With this declaration, there will not be assignment of any memory to the structure object. Malloc function should be used to allocate memory and below is the syntax to do so:

Struct abc *ptr;

ptr=malloc(sizeof(struct abc));

Now, ptr will have the memory assigned. It can store the data for member elements of structure.

To access the structure members with the help of pointers can be done as follows:

Ptr->a = 10; ptr->b = ‘c’; ptr->c = 2.0;

So far, we discussed the single structure object. Both the ways of access of member elements, we have seen through pointers and the direct access with objects.

We have two ways to define the structures, first is to define the structure object and another way is to define the pointer to the structure.

Structure object: Struct abc obj;

Structure pointer: Struct abc *ptr;

Now, let us discuss the array of structure objects and array of structure pointers. Array is a group of objects of same type. For example array of obj will be declared as struct abc obj[128]. Array of pointers to the structure objects will be as struct abc *ptr[128]. Both the array defined the 128 elements of structure objects and pointers.

C program to declare structure object:

#include
struct abc{
        int a;
        char b;
        float c;
};
int main()
{
        struct abc obj = {1,'c',3.4};
printf("a=%d,b=%c,c=%f\n",obj.a,obj.b,obj.c);
        return 0;
}

Output:

bash-4.2$ ./a.out

a=1,b=c,c=3.400000

bash-4.2$

Above program defines the object of structure and a way to initialize the member while declaring the object. We initialize the member variables with specific values and print those variables by accessing the members with object directly. a is assigned with 1, b is assigned with ‘c’ and c is assigned with float value 3.4. Below is the snapshot of the program and output.

Snapshot:

Graphical user interface, text, application, email Description automatically generated

Graphical user interface, text, application Description automatically generated

C program to declare the pointer to the structure:

#include
struct abc{
        int a;
        char b;
        float c;
};
int main()
{
        struct abc *ptr;
ptr = malloc(sizeof(struct abc));
ptr->a = 4;
ptr->b = 'd';
ptr->c = 5.5;
printf("a=%d,b=%c,c=%f\n",ptr->a,ptr->b,ptr->c);
        return 0;
}

Output:

bash-4.2$ ./a.out

a=4,b=d,c=5.500000

bash-4.2$

Above program defines the pointer to object of structure. Malloc function is used to allocate memory for the pointer variable. We initialize the member variables with specific values and print those variables by accessing the members with pointer. a is assigned with 4, b is assigned with ‘d’ and c is assigned with float value 5.5. Below is the snapshot of the program and output.

Snapshot:

Graphical user interface, text, application Description automatically generated

Graphical user interface, text, application Description automatically generated

Now, let us go through the C program for array of structures and array of pointers to structures.

C program for array of structure of objects:

#include
struct abc{
        int a;
        char b;
        float c;
};
int main()
{
        struct abcobj[2];
obj[0].a = 4;
obj[0].b = 'd';
obj[0].c = 5.5;
obj[1].a = 5;
obj[1].b = 'f';
obj[1].c = 8.2;
printf("[0]a=%d,[0]b=%c,[0]c=%f\n",obj[0].a,obj[0].b,obj[0].c);
printf("[1]a=%d,[1]b=%c,[1]c=%f\n",obj[1].a,obj[1].b,obj[1].c);
        return 0;
}

Output:

bash-4.2$ ./a.out

[0]a=4,[0]b=d,[0]c=5.500000

[1]a=5,[1]b=f,[1]c=8.200000

bash-4.2$

Above program defines the array of object of structure and a way to initialize the members with the help of objects. We initialize the member variables with specific values and print those variables by accessing the members with object directly. For simplicity, we have taken only 2 objects. First object’s a is assigned with 4, b is assigned with ‘d’ and c is assigned with float value 5.5. second object’s a is assigned with 5, b is assigned with ‘f’ and c is assigned with float value 8.2. Below is the snapshot of the program and output.

Snapshot:

Text Description automatically generated

Graphical user interface, text, application Description automatically generated

C program for array of pointers to structure of objects:

#include
struct abc{
        int a;
        char b;
        float c;
};
int main()
{
        struct abc *ptr[2];
ptr[0] = malloc(sizeof(struct abc));
ptr[1] = malloc(sizeof(struct abc));
ptr[0]->a = 4;
ptr[0]->b = 'd';
ptr[0]->c = 5.5;
ptr[1]->a = 5;
ptr[1]->b = 'f';
ptr[1]->c = 8.2;
        printf("[0]a=%d,[0]b=%c,[0]c=%f\n",ptr[0]->a,ptr[0]->b,ptr[0]->c);
        printf("[1]a=%d,[1]b=%c,[1]c=%f\n",ptr[1]->a,ptr[1]->b,ptr[1]->c);
        return 0;
}

Output:

bash-4.2$ ./a.out

[0]a=4,[0]b=d,[0]c=5.500000

[1]a=5,[1]b=f,[1]c=8.200000

bash-4.2$

Above program defines the array of pointers to object of structure and a way to initialize the members with the help of pointers. We initialize the member variables with specific values and print those variables by accessing the members with pointer variables. For simplicity, we have taken only 2 pointers. First pointer to object a is assigned with 4, b is assigned with ‘d’ and c is assigned with float value 5.5. second pointer to object a is assigned with 5, b is assigned with ‘f’ and c is assigned with float value 8.2. Below is the snapshot of the program and output.

Snapshots:

Text Description automatically generated

Graphical user interface, text, application Description automatically generated

Conclusion:

We discussed the structure data type in C and the ways to declare the objects and pointers to structure objects. We also discussed few examples and with output. Both the objects and pointers were discussed. Array of objects and pointer to objects also were discussed with examples.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com