C - Structures and Arrays

C Programming

Share

In this tutorial we will learn to use structures with arrays in C programming language.

We have already covered arrays and structures in previous tutorials.

Create an array variable for a given structure

In the following example we are creating a structure student to hold student detail.

struct student {
  char firstname[64];
  char lastname[64];
  char id[64];
  int score;
};

In the previous tutorial we created a single student structure variable by the name std1. Now we will create a student structure array variable stdArr

In the following example we are creating a student structure array variable stdArr to hold details of 3 students so the size of the array is 3.

struct student stdArr[3];

Accessing members of a structure array variable

To access a member of a structure array variable we first select the index then we target the member.

In the following example we are selecting the first element of the structure array variable stdArr and then targeting the firstname member.

stdArr[0].firstname

Array indexing starts from 0 so, the first element of the array is at index 0.

Write a program in C to collect details of 3 students and print the result

In this example we will be using the student structure to create an array variable stdArr of size 3 to hold details of 3 students.

#include <stdio.h>

int main(void) {
  // creating a student structure template
  struct student {
    char firstname[64];
    char lastname[64];
    char id[64];
    int score;
  };
  
  // creating a student structure array variable
  struct student stdArr[3];
  
  // other variables
  int i;
  
  // taking user input
  for (i = 0; i < 3; i++) {
    printf("Enter detail of student #%d\n", (i+1));

    printf("Enter First Name: ");
    scanf("%s", stdArr[i].firstname);
  
    printf("Enter Last Name: ");
    scanf("%s", stdArr[i].lastname);
  
    printf("Enter ID: ");
    scanf("%s", stdArr[i].id);
  
    printf("Enter Score: ");
    scanf("%d", &stdArr[i].score);
  }
  
  // output
  for (i = 0; i < 3; i++) {
    printf("\nStudent #%d Detail:\n", (i+1));
    printf("Firstname: %s\n", stdArr[i].firstname);
    printf("Lastname: %s\n", stdArr[i].lastname);
    printf("ID: %s\n", stdArr[i].id);
    printf("Score: %d\n", stdArr[i].score);
  }
  
  return 0;
}

In the above code we are using & like &stdArr[i].score when taking integer value. For string input we don't need the ampersand so, we have std[i].firstname, std[i].lastname and std[i].id.

Output:

Enter detail of student #1
Enter First Name: Bruce
Enter Last Name: Wayne
Enter ID: dc-01
Enter Score: 8
Enter detail of student #2
Enter First Name: Peter
Enter Last Name: Parker
Enter ID: mc-01
Enter Score: 9
Enter detail of student #3
Enter First Name: Tony
Enter Last Name: Stark
Enter ID: mc-02
Enter Score: 7

Student #1 Detail:
Firstname: Bruce
Lastname: Wayne
ID: dc-01
Score: 8

Student #2 Detail:
Firstname: Peter
Lastname: Parker
ID: mc-01
Score: 9

Student #3 Detail:
Firstname: Tony
Lastname: Stark
ID: mc-02
Score: 7
Share