Tsallaka zuwa abun ciki
Justice Tech School

Arrays of Structures

An array of structs stores many records.

Example
#include <stdio.h>

typedef struct { const char *name; int qty; } Item;
int main(void) {
  Item items[] = {{"pen", 3}, {"book", 5}};
  int total = 0;
  for (int i = 0; i < 2; i++) { printf("%s ", items[i].name); total += items[i].qty; }
  printf("%d\n", total);
  return 0;
}

Result

pen book 8

Exercise

Total quantity.

total += items[i].

Quiz Test

How many items are here?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.