C Programming

How to Use Malloc Function to Create Array of Structs

The struct is a data type similar to the array used in the c programming, but the only difference is that an array contains the values of the same data type whereas the struct contains the values on the basis of user-defined data types. The arrays occupied some spaces on the memory of the system which can be either dynamic or static. The malloc() function is used for the declaration of the dynamic memory.

An array of a struct can be declared either using the static memory or dynamic memory, in this write-up, we will discuss the array of structs using the malloc() function.

How to create an array of structs with malloc function in C

The structs in C programming are used similarly to classes. The execution time of the structs is relatively faster than the classes. To understand the struct, consider the example:

1
2
3
4
5
6
7
struct employees{

int emp_id;

char emp_name;

};

We have a struct of “employees” which has two further members; emp_int and emp_char. We can form an array using the struct employees as:

1
struct employees employeesData[4];

We have declared an array “employeesData” using the struct “employees” and it has 4 locations to store the values. If we want to access the second elements of the struct array we will use, employeesData[1], and similarly if we want to access the members of the elements we will use, employeesData[1].emp_id.

But here we can also use the malloc() function to use the dynamic memory allocation. The advantage of dynamic memory is that it utilizes the space during the execution of the program according to the requirement. For this, we use the malloc() function, which sends the request to assign a memory block to the heap segment of the memory which stores the data randomly. If the heap memory cannot entertain the request of malloc() due to insufficient space, it returns the null else it assigns the requested block to the malloc() function for the execution of the program.

Now if we want to use the malloc() function for the declaration of the array of struct, the program will be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char** argv)

{

    typedef struct
    {
        char* emp_name;
        int emp_id;
    } Employees;

    int num=2,i;
    Employees* employeesData = malloc(num * sizeof *employeesData);

    for (i = 0; i < num; i++)
    {
        employeesData[i].emp_name=(char*)malloc(sizeof(char*));
       
        printf("Enter employee name :");
        scanf("%s",employeesData[i].emp_name);

        printf("Enter employee id  :");
        scanf("%d",&employeesData[i].emp_id);

    }
printf(“Please Enter All the Names of Maximum 8 Character \n”);
    for (i = 0; i < num; i++)

        printf("Employee Name: %s, Employees Id: %d\n",employeesData[i].emp_name,employeesData[i].emp_id);
free(employeesData);
    return (0);}

We will open a text file, myfile1, with the help of nano editor and paste the above script:

1
$ nano myfile1.c

Use the GCC compiler to compile the above file:

1
$ gcc myfile1.c -o myfile1

Once the file is successfully compiled, run the program using the command:

1
$ ./myfile1

The explanation of the above code is:

  • First, we have added the libraries of stdlib.h(used for the dynamic memory allocation functions) and stdio.h (used by the other basic functions of the C programming)
  • Then in the main function, we passed the argc(argument count) and argv(argument vector) used for the numbers which are input by the users and point to the character pointers respectively
  • After this, we have declared the struct of “Employees” having two values emp_id and emp_name
  • Initialized the two variables num and i; num has assigned the value of 2 so that it can take two inputs for struct “Employees”
  • Then used the malloc function to assign the memory according to the value of num to the pointer array (Employees)
  • Took the input from the user and display the values using the for loop

Note: We have to use the “typedef struct” in declaring the struct, by using this we do not have to use the keyword “struct” repeatedly.

Another thing to be noted is that it can cause the buffer to overflow because we have used the “char *” inside the malloc argument to declare the size of the emp_name variable. This causes the emp_name to be limited to 8 bytes for 64-bit architecture and 4- bytes for 32-bit architectures. This buffer overflow doesn’t occur all the time (most of the time it is automatically managed by the compiler at run time) therefore we have prompted the user to enter 8 characters max for the employee name just to be on the safe side.

If you don’t want to put a limit on the number of characters for the employee name then you can simply take the input first for the employee name inside the for loop within a new variable and then pass that variable size in the malloc function.

Conclusion

The struct data type in C programming provides better performance when we have to deal with small groups of the same values. In this write-up, we have discussed the creation of structs with arrays using the dynamic memory function that is malloc() function. The malloc() function uses only that much block of memory which is required.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.