Array of Structures in C
Hello Everyone, In this article, we will discuss Array of Structures in C Programming. This concept allows us to store and manage collections of structured data efficiently. By combining arrays, which hold multiple elements of the same type, with structures that organize different data types, we can build more versatile and organized programs. Whether you are new to C or have some experience, this guide will help you to understand basics of Array of Structures in C effectively with examples. Let’s get started.
In the C programming language, an array of structures is a collection of elements of the same structure type stored in a contiguous block of memory. A structure is a user-defined data type that groups together variables of different data types under a single name. Arrays and structures are both important concepts in C, and combining them allows you to create a powerful and organized data structure.
Here’s a simple example to illustrate an array of structures:
#include <stdio.h>// Define a structure to represent a student
typedef struct {
int roll_no;
char name[50];
float gpa;
} Student;
int main() {
// Declare and initialize an array of 3 student structures
Student students[3] = {
{1, "Alice", 3.8},
{2, "Bob", 3.6},
{3, "Carol", 3.9}
};
// Access and print the data from the array of structures
for (int i = 0; i < 3; i++) {
printf("Student %d: Roll No = %d, Name = %s, GPA = %.1f\\n", i+1, students[i].roll_no, students[i].name, students[i].gpa);
}
return 0;
}
In this example, a structure called Student is defined, which contains an integer roll_no, a character array name of length 50, and a float gpa. Then, an array of 3 Student structures called students is declared and initialized. Finally, the data from the students array is accessed and printed using a loop.
Here’s an example of an array of structures representing daily weather records:
#include <stdio.h>// Define a structure to represent weather information
typedef struct {
int day;
int month;
int year;
float temperature;
float humidity;
float precipitation;
} WeatherRecord;
int main() {
// Declare and initialize an array of 5 weather records
WeatherRecord records[5] = {
{1, 4, 2023, 68.0, 45.0, 0.0},
{2, 4, 2023, 71.2, 50.0, 0.1},
{3, 4, 2023, 66.5, 55.0, 0.0},
{4, 4, 2023, 69.3, 48.0, 0.2},
{5, 4, 2023, 73.1, 43.0, 0.0}
};
// Access and print the data from the array of structures
for (int i = 0; i < 5; i++) {
printf("Weather Record for %02d-%02d-%d: Temperature = %.1fF, Humidity = %.1f%%, Precipitation = %.1fmm\\n",
records[i].day, records[i].month, records[i].year, records[i].temperature, records[i].humidity, records[i].precipitation);
}
return 0;
}
In this example, a structure called WeatherRecord is defined, which contains integers for day, month, and year, and floats for temperature, humidity, and precipitation. Then, an array of 5 WeatherRecord structures called records is declared and initialized. Finally, the data from the records array is accessed and printed using a loop.
This daily life example demonstrates how you can use an array of structures to represent and manage weather data for a sequence of days.
output
Weather Record for 01-04-2023: Temperature = 68.0F, Humidity = 45.0%, Precipitation = 0.0mm
Weather Record for 02-04-2023: Temperature = 71.2F, Humidity = 50.0%, Precipitation = 0.1mm
Weather Record for 03-04-2023: Temperature = 66.5F, Humidity = 55.0%, Precipitation = 0.0mm
Weather Record for 04-04-2023: Temperature = 69.3F, Humidity = 48.0%, Precipitation = 0.2mm
Weather Record for 05-04-2023: Temperature = 73.1F, Humidity = 43.0%, Precipitation = 0.0mm
This output displays the weather records for five consecutive days, showing the temperature in Fahrenheit, humidity in percentage, and precipitation in millimetres for each day.