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:
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:
struct employees employeesData;
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, and similarly if we want to access the members of the elements we will use, employeesData.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:
int main(int argc, char** argv)
Employees* employeesData = malloc(num * sizeof *employeesData);
for (i = 0; i < num; i++)
printf("Enter employee name :");
printf("Enter employee id :");
for (i = 0; i < num; i++)
printf("Employee Name: %s, Employees Id: %d\n",employeesData[i].emp_name,employeesData[i].emp_id);
We will open a text file, myfile1, with the help of nano editor and paste the above script:
Use the GCC compiler to compile the above file:
$ gcc myfile1.c -o myfile1
Once the file is successfully compiled, run the program using the command:
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.
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.