Skip to main content

Structures and Arrays in C

Structures and Arrays

Structures and arrays can be used together to represent complex data structures that require multiple values of different data types to be stored together.

One common way to use structures and arrays together is to create an array of structures, where each element of the array is a structure.

Example - Array of structures

#include <stdio.h>
#include <string.h>

// Define a structure to represent a person
struct person
{
char name[50];
int age;
float height;
};

int main()
{
// Declare an array of person structures
struct person people[3];

// Set values for the first person
strcpy(people[0].name, "John");
people[0].age = 30;
people[0].height = 6.0;

// Set values for the second person
strcpy(people[1].name, "Jane");
people[1].age = 25;
people[1].height = 5.5;

// Set values for the third person
strcpy(people[2].name, "Bob");
people[2].age = 40;
people[2].height = 5.10;

// Print out the values for each person
for (int i = 0; i < 3; i++)
{
printf("Person %d: name=%s, age=%d, height=%.2f\n", i + 1,
people[i].name, people[i].age, people[i].height);
}

return 0;
}
Output:

Explanation

  • We define a person structure with members for the person's name, age, and height.
  • We then declare an array of person structures called people, which can hold up to 3 people.
  • We set the values for each person in the array using the dot operator and the array index.
  • For example, to set the name of the first person in the array, we use the strcpy function to copy the string "John" into the name member of people[0].
  • Finally, we use a for loop to iterate through the people array and print out the values for each person using the printf function.
  • We use the dot operator and the array index to access the members of each person structure.

Output:

Person 1: name=John, age=30, height=6.00
Person 2: name=Jane, age=25, height=5.50
Person 3: name=Bob, age=40, height=5.10

Other Complex Uses

There are several ways to use arrays and structures together in C, depending on the specific requirements of the program, some of these are even Complex:

Structure of arrays

  • In this approach, a structure is used to group together arrays of related data.

For example

If we want to store data for a single employee, we can define a structure with members for employee name, ID, and salary, and then create arrays to store data for all the employees.

struct employee {
char name[50];
int id;
float salary;
};

char names[10][50]; // array of 10 employee names
int ids[10]; // array of 10 employee IDs
float salaries[10]; // array of 10 employee salaries

struct employee emp = { names, ids, salaries }; // structure of arrays

Array of structures containing arrays

  • In this approach, an array of structures is used to store multiple records of data, where each record contains multiple values of the same data type.

For example

If we want to store data for multiple students, where each student has an array of grades, we can define a structure with members for student name and an array of grades, and then create an array of structures to store data for all the students.

struct student {
char name[50];
int grades[5];
};

struct student students[10]; // array of 10 student structures
note

These are just a few examples of the ways in which arrays and structures can be used together in C. The specific approach that is most appropriate for a given program will depend on the data that needs to be stored and the operations that will be performed on that data.