Array memory Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

The ideas behind the lab

Exactly what you watched happen, in plain steps.

One name, many cells

An array is a fixed number of variables of the same type, stored next to each other in memory. The size is fixed when it is declared. Each element is reached with an index in square brackets: the first is [0], the last is [size − 1]. Because the cells are side by side, the address of a[i] is simply start + i × the size of one element.

int a[5];                        // 5 ints, garbage values
int b[5] = {10, 20, 30};         // 10 20 30 0 0
int c[] = {4, 7, 1};             // size counted: 3
int d[5] = {0};                  // all zeros

b[1] = 99;                       // write   (2nd element)
printf("%d", b[0]);              // read    (1st element)
int n = sizeof b / sizeof b[0];  // 5 elements
int a[5] = {10, 20, 30} in memory (start address 0x1000)
Element Value Address Offset
a[0] 10 0x1000 0 × 4 = 0 bytes
a[1] 20 0x1004 1 × 4 = 4 bytes
a[2] 30 0x1008 2 × 4 = 8 bytes
a[3] 0 0x100c 3 × 4 = 12 bytes
a[4] 0 0x1010 4 × 4 = 16 bytes
a[5] — not part of a — 0x1014 out of bounds!

Declaring and initialising

  1. type name[size]; — the size must be a positive whole number.
  2. Values in { } fill the cells from the start; missing ones become 0.
  3. With an initialiser list you may leave the size out: int c[] = {4, 7, 1};
  4. Without any initialiser, a local array holds garbage.
  5. An array cannot be assigned as a whole: b = a; is an error — copy element by element.

Indexes

  1. The first index is 0, the last is size − 1.
  2. The index can be any int expression: a[i], a[i + 1], a[n - 1].
  3. C does NOT check the index: a[5] in an int a[5] reads or writes someone else's memory.
  4. Out-of-bounds access is undefined behaviour: wrong values, changed variables or a crash.
  5. Loop with i = 0; i < size — never i <= size.

Classic array loops

  1. Traversal: for (i = 0; i < n; i++) visits every element once.
  2. Sum / average: an accumulator; divide by n as a double.
  3. Max / min: start with a[0], then compare every element.
  4. Linear search: compare one by one, break at the first match, -1 if none.
  5. Reverse: swap a[i] and a[j] while i < j, moving both towards the middle.
Walkthrough

Average marks and how many are above it

Every line, explained.

1 #include <stdio.h>
2 #define N 5 The size in one place: change it here only.
3
4 int main(void)
5 {
6 int marks[N] = {72, 85, 60, 91, 78};
7 int sum = 0;
8 for (int i = 0; i < N; i++) i = 0, 1, 2, 3, 4 — every valid index.
9 sum += marks[i]; sum = 386
10 double avg = (double) sum / N; The cast avoids integer division.
11 printf("average %.1f\n", avg); Prints average 77.2
12 int above = 0;
13 for (int i = 0; i < N; i++) A second pass over the same array.
14 if (marks[i] > avg) above++; 85, 91 and 78
15 printf("%d above average\n", above); Prints 3 above average
16 printf("%zu bytes\n", sizeof marks); Prints 20 bytes (5 × 4)
17 return 0;
18 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

Looping one element too far

✗ Wrong
for (i = 0; i <= 5; i++) a[i] = 0;   // a has 5 elements
✓ Right
for (i = 0; i < 5; i++) a[i] = 0;

a[5] does not exist. Writing it changes other memory — no error message, just strange bugs.

Starting at index 1

✗ Wrong
for (i = 1; i <= n; i++)
    sum += a[i];
✓ Right
for (i = 0; i < n; i++)
    sum += a[i];

This skips a[0] and reads a[n], which is out of bounds.

Copying an array with =

✗ Wrong
int a[3] = {1, 2, 3}, b[3];
b = a;   // error
✓ Right
for (i = 0; i < 3; i++)
    b[i] = a[i];

An array name is not a variable you can assign to. Copy the elements one by one (or use memcpy).

Using an uninitialised array

✗ Wrong
int count[10];
count[3]++;   // garbage + 1
✓ Right
int count[10] = {0};
count[3]++;

A local array without an initialiser holds whatever was in memory. = {0} sets every element to 0.

Starting the maximum at 0

✗ Wrong
int max = 0;   // wrong for {-5, -2, -9}
✓ Right
int max = a[0];

If all values are negative, 0 is never beaten and max stays 0 — a value that is not even in the array.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

int a[6]; — what is the last valid index?

Question 2

int a[4] = {3, 8}; — what is a[3]?

Question 3

int a[] = {5, 1, 4}; — how many bytes is sizeof a (4-byte int)?

Question 4

What does C do when you write a[10] in an int a[5]?

Question 5

int a[] = {2, 4, 6}; int s = 0; for (int i = 0; i < 3; i++) s += a[i]; — s is?

Assessment

Arrays quiz

Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.