Pointer arithmetic 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.

An array name becomes a pointer

When you use an array name in an expression, C turns it into a pointer to its first element: a means &a[0]. Adding an integer to a pointer moves it by whole elements — p + 1 is the next int, 4 bytes further — so *(p + i) is element i. That is exactly what a[i] means: a[i] is defined as *(a + i). Pointers into the same array can also be compared and subtracted.

int a[5] = {10, 20, 30, 40, 50};
int *p = a;          // p = &a[0]

*(p + 2)             // 30 — same as a[2] and p[2]
p++;                 // p → a[1]  (address + 4 bytes)
p - a                // 1 element

for (p = a; p < a + 5; p++)
    printf("%d ", *p);
int a[5]; int *p = a; (a starts at 0x1000)
Expression Same as Value / address
a &a[0] 0x1000
a + 3 &a[3] 0x100c (3 × 4 bytes on)
*(a + 3) a[3] the 4th element
p[i] *(p + i) element i counted from p
p++ p = p + 1 next element (+4 bytes for int, +1 for char, +8 for double)
q - p — number of elements between them

Pointer arithmetic

  1. p + n moves n elements: n × sizeof(*p) bytes.
  2. p++ and p-- step one element forward or back.
  3. q - p (same array) gives the number of elements between them.
  4. p < q compares positions in the same array.
  5. You may point one past the last element, but never dereference it.

Arrays vs pointers

  1. a[i] and *(a + i) are the same thing; so are p[i] and *(p + i).
  2. An array name cannot be changed: a++ is an error; p++ is fine.
  3. sizeof a gives the whole array; sizeof p gives 8 (just the pointer).
  4. Passing an array to a function passes &a[0] — the parameter is really a pointer.
  5. So int f(int a[]) and int f(int *a) mean the same.

Pointers and strings

  1. A char * can walk a string: while (*p) p++; stops at '\0'.
  2. p - s is then the length.
  3. char s[] = "hi"; is an array you may change.
  4. char *t = "hi"; points to a string literal — do not change it (const char * is better).
  5. Two pointers moving towards each other can reverse a string in place.
Walkthrough

Reverse a string with two pointers, print part of an array

Every line, explained.

1 #include <stdio.h>
2 #include <string.h>
3
4 void reverse(char *s)
5 {
6 char *left = s; The first character.
7 char *right = s + strlen(s) - 1; The last character (before '\0').
8 while (left < right) { Until they meet.
9 char t = *left;
10 *left++ = *right; Store, then move left forward.
11 *right-- = t; Store, then move right back.
12 }
13 }
14
15 void printAll(const int *p, int n) Receives any part of an int array.
16 {
17 for (int i = 0; i < n; i++)
18 printf("%d ", *(p + i));
19 printf("\n");
20 }
21
22 int main(void)
23 {
24 char word[] = "stressed";
25 reverse(word);
26 printf("%s\n", word); Prints desserts
27 int a[] = {5, 10, 15};
28 printAll(a, 3); Prints 5 10 15
29 printAll(a + 1, 2); Starts at a[1]: prints 10 15
30 return 0;
31 }
Common mistakes

Spot the bug

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

Thinking p + 1 is the next byte

✗ Wrong
int *p = a;
int *q = p + 4;   // "4 bytes on"?
✓ Right
int *p = a;
int *q = p + 1;   // the next int (4 bytes on)

Pointer arithmetic counts elements; C multiplies by the element size for you.

Changing the array name

✗ Wrong
int a[5];
a++;          // error
✓ Right
int *p = a;
p++;

An array name is not a variable; it always means the start of the array.

Dereferencing one past the end

✗ Wrong
for (p = a; p <= a + 5; p++) sum += *p;
✓ Right
for (p = a; p < a + 5; p++) sum += *p;

a + 5 may be used for comparing, but there is no element there to read.

Changing a string literal

✗ Wrong
char *s = "hello";
s[0] = 'H';   // crash
✓ Right
char s[] = "hello";
s[0] = 'H';

A pointer to a literal points into read-only memory. An array gets its own copy.

sizeof on a pointer

✗ Wrong
int *p = a;
int n = sizeof p / sizeof *p;   // 2, not 5
✓ Right
int n = sizeof a / sizeof a[0];   // only on the array itself

sizeof p is the size of the pointer (8), not of the array it points into.

Practice

Check your understanding

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

0 / 5 correct
Question 1

int a[] = {4, 8, 12}; int *p = a; — what is *(p + 1)?

Question 2

If an int pointer p holds 0x1000, what does p + 2 hold (4-byte int)?

Question 3

Which expression is the same as a[3]?

Question 4

char s[] = "code"; char *p = s; while (*p) p++; — what is p - s?

Question 5

int a[5]; — which statement is an error?

Assessment

Pointers & 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.