Array of Structures in C

Level 4193 keystrokes

Creates an array of structures and prints each element's members in a loop.

C code

// Structure type with a name and a score
1struct Student {
// Fixed-size char array: up to 19 characters plus '\0'
2 char name[20];
3 int score;
4};
5
// Each inner {} initializes one Student; the size 2 is inferred
6struct Student students[] = {{"Alice", 90}, {"Bob", 85}};
// Visit both elements (i = 0, 1)
7for (int i = 0; i < 2; i++) {
// students[i].name picks a member of element i: Alice: 90, then Bob: 85
8 printf("%s: %d\n", students[i].name, students[i].score);
9}

Output

Alice: 90
Bob: 85
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • ;× 7
  • "× 6
  • ,× 5
  • {× 5
  • }× 5
  • [× 4
  • ]× 4
  • .× 2
  • (× 2
  • )× 2

More C examples at level 4

← C typing practice