Array of Structs in C
This tutorial introduces how to create an array of structures in C. It is the collection of multiple structure variables where each variable contains information about different entities.
Array of struct
in C
An array is a sequential collection of the same data type, and a structure is a user-defined data type. The declaration of an array of structures is the same as an array of the primitive data types but uses the structure has its elements’ data type.
Consider an example of a structure named Student
as below:
struct Student
{
int rollNumber;
char studentName[10];
float percentage;
};
We can declare an array of structures as below.
struct Student studentRecord[5];
Here, the studentRecord
is an array of 5 elements where each element is of type struct
Student
. The individual elements are accessed using the index notation ([]
), and members are accessed using the dot .
operator.
The studentRecord[0]
points to the 0th
element of the array, and the studentRecord[1]
points to the 1st
element of the array.
Similarly,
- The
studentRecord[0].rollNumber
refers to therollNumber
member of the 0th element of the array. - The
studentRecord[0].studentName
refers to thestudentName
member of the 0th element of the array. - The
studentRecord[0].percentage
refers to thepercentage
member of the 0th element of the array.
The complete program to declare an array of the struct
in C is as follows.
#include<stdio.h>
#include<string.h>
struct Student
{
int rollNumber;
char studentName[10];
float percentage;
};
int main(void)
{
int counter;
struct Student studentRecord[5];
printf("Enter Records of 5 students");
for(counter=0; counter<5; counter++)
{
printf("\nEnter Roll Number:");
scanf("%d",&studentRecord[counter].rollNumber);
printf("\nEnter Name:");
scanf("%s",&studentRecord[counter].studentName);
printf("\nEnter percentage:");
scanf("%f",&studentRecord[counter].percentage);
}
printf("\nStudent Information List:");
for(counter=0; counter<5; counter++)
{
printf("\nRoll Number:%d\t Name:%s\t Percentage :%f\n",
studentRecord[counter].rollNumber,studentRecord[counter].studentName, studentRecord[counter].percentage);
}
return 0;
}
Output:
Enter Record of 5 students
Enter Roll number:1
Enter Name: John
Enter percentage: 78
Enter Roll number:2
Enter Name: Nick
Enter percentage: 84
Enter Roll number:3
Enter Name: Jenny
Enter percentage: 56
Enter Roll number:4
Enter Name: Jack
Enter percentage: 77
Enter Roll number:5
Enter Name: Peter
Enter percentage: 76
Student Information List
Roll Number: 1 Name: John percentage:78.000000
Roll Number: 2 Name: Nick percentage:84.000000
Roll Number: 3 Name: Jenny percentage:56.000000
Roll Number: 4 Name: Jack percentage:77.000000
Roll Number: 5 Name: Peter percentage:76.000000
Create an Array of struct
Using the malloc()
Function in C
There is another way to make an array of struct
in C. The memory can be allocated using the malloc()
function for an array of struct
. This is called dynamic memory allocation.
The malloc()
(memory allocation) function is used to dynamically allocate a single block of memory with the specified size. This function returns a pointer of type void
.
The returned pointer can be cast into a pointer of any form. It initializes each block with the default garbage value.
The syntax of the malloc()
function is as below:
ptrVariable = (cast-type*) malloc(byte-size)
The complete program to create an array of struct dynamically is as below.
#include<stdio.h>
int main(int argc, char** argv)
{
typedef struct
{
char* firstName;
char* lastName;
int rollNumber;
} STUDENT;
int numStudents=2;
int x;
STUDENT* students = malloc(numStudents * sizeof *students);
for (x = 0; x < numStudents; x++)
{
students[x].firstName=(char*)malloc(sizeof(char*));
printf("Enter first name :");
scanf("%s",students[x].firstName);
students[x].lastName=(char*)malloc(sizeof(char*));
printf("Enter last name :");
scanf("%s",students[x].lastName);
printf("Enter roll number :");
scanf("%d",&students[x].rollNumber);
}
for (x = 0; x < numStudents; x++)
printf("First Name: %s, Last Name: %s, Roll number: %d\n",students[x].firstName,students[x].lastName,students[x].rollNumber);
return (0);
}
Output:
Enter first name:John
Enter last name: Williams
Enter roll number:1
Enter first name:Jenny
Enter last name: Thomas
Enter roll number:2
First Name: John Last Name: Williams Roll Number:1
First Name: Jenny Last Name: Thomas Roll Number:2