Theory
The question that fails half the class
An int pointer p holds address 5000. What is p + 1?
Half of every class answers 5001, and loses the mark.
The machine answer: 5004. Because p is an int*, and an int occupies 4 bytes, C decided that pointer arithmetic should count in whole boxes, not bytes. That single design choice is today's lesson, and it is the reason pointers and arrays are about to become one topic.
Theory
Walking past houses, not bricks
Walking down a street, "next house" does not mean "one brick to the right": it means one FULL PLOT ahead, whatever the plot size. Pointer arithmetic walks the same way: p + 1 means the NEXT BOX of p's type. Int plots are 4 bytes wide, char plots 1, so the same +1 stride covers different distances. The pointer's type is the plot size on the slip.
Theory
The arithmetic, formally
For a pointer p of type T*, with sizeof(T) = s:
- p + n → the address n boxes ahead: numerically
p + n×sbytes. - p - n → n boxes back.
- p++ / p-- → step one box forward/back (the walking engine).
- q - p (both into the same array) → how many ELEMENTS lie between them, not bytes.
Meaningless and illegal: p + q (adding two addresses answers nothing), and multiplying or dividing pointers. C permits exactly the operations that mean something on a street of boxes.
Practical
Watch the strides differ
#include <stdio.h>
int main() {
int marks[3] = {70, 80, 90};
int *p = &marks[0];
char word[] = "abc";
char *c = &word[0];
printf("p = %p\n", (void *)p);
printf("p+1 = %p\n", (void *)(p + 1)); /* +4 bytes */
printf("c = %p\n", (void *)c);
printf("c+1 = %p\n", (void *)(c + 1)); /* +1 byte */
printf("*(p+2) = %d\n", *(p + 2));
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
A float pointer pf holds address 6000 (floats are 4 bytes). What address is pf + 3?
- 6012
- 6003
- 6004
- 6300
Show the answer
6012
Three float-boxes ahead: 6000 + 3×4 = 6012. Answering 6003 counts bytes instead of boxes, the classic slip this whole lesson exists to kill. Formula for every such question: address + n × sizeof(type). With char* the two answers coincide (stride 1), which is why exams prefer int and float.
Think first
Decode *(p + 2)
In the code above, p points at marks[0] of {70, 80, 90}. Read *(p + 2) aloud using both operators' meanings, then predict what it prints.
Show the answer
"Go two int-boxes ahead of p, then fetch the value living there." Two boxes past marks[0] is marks[2], so it prints 90.
Sit with this: *(p + 2) behaved exactly like marks[2]. That is not coincidence, it is next lesson's headline. You have just used array indexing written in pointer clothing.
Quiz
p points at marks[1] and q points at marks[4] of the same int array. What is q - p?
- 3, the number of elements between them
- 12, the number of bytes between them
- 5, the higher index
- Illegal: pointers cannot be subtracted
Show the answer
3, the number of elements between them
Pointer difference is scaled DOWN by the element size, answering the useful question "how many boxes apart?": 4 - 1 = 3. The byte gap (12) is the raw arithmetic C hides from you. And note the asymmetry exams probe: p - q is legal within an array; p + q is meaningless and illegal.
Watch out
Where marks leak
Byte-counting: p+1 on an int moves 4, not 1; write the ×sizeof step in every answer. Adding pointers: p + q is illegal, only differences make sense. Walking off the street: p++ past the array's end is out-of-bounds with a pointer accent, same silent corruption as marks[n]. And %p with a (void ) cast is the honest way to PRINT an address, worth using in lab work.
Theory
The reveal ahead
You caught it in the reveal: *(p + 2) IS marks[2]. Next lesson makes it official: an array's NAME is the address of box zero, indexing is pointer arithmetic in disguise, and the & mystery of scanf("%s", name) closes forever. The two biggest ideas of this unit are about to shake hands.
Summary
Key takeaways
- Pointer arithmetic counts in boxes: p + n advances n × sizeof(type) bytes.
- int at 5000: p+1 is 5004; char strides 1; float* strides 4.
- p++ walks box by box, the traversal engine.
- q - p (same array) counts ELEMENTS between; p + q is illegal.
- *(p + 2) fetches the value two boxes ahead, and equals marks[2].
- Memory hook: next house, not next brick.