ABSTRACT

A pointer is simply a variable that holds a memory address. To navigate through data, C uses Pointer Arithmetic, where adding an integer to a pointer moves it by a multiple of the size of the type it points to.

1. Address Calculation (Scaling)

When you index into a pointer or perform arithmetic, C automatically scales the offset.

Formula for Address:

Example Comparison: int32_t vs uint8_t

Pointer TypeBase AddressOperationSize of TypeResulting Address
int32_t* a0x100a + 34 bytes0x10C ()
uint8_t* b0x200b + 31 byte0x203 ()

2. Array Syntax vs. Pointer Arithmetic

In C, the array subscript operator [] is actually syntactic sugar for pointer arithmetic and dereferencing.

  • The Equivalence: a[i] is exactly the same as *(a + i).
  • The Pointer: A pointer does not represent an array; it only represents a starting address. It has no internal knowledge of the “size” or “length” of the data it points to.
Array SyntaxPointer ArithmeticAction
a[0]*aGet the value at the base address.
a[3]*(a + 3)Get the value 3 elements away.
&a[5]a + 5Get the address 5 elements away.

3. Why Use Pointer Arithmetic?

While array syntax is often more readable, pointer arithmetic is powerful when dealing with offsets and sub-sections of data.

TIP

Functional Advantage: If a standard library function (like strlen or strcpy) takes a starting pointer, you can pass a + 5 to make the function ignore the first 5 elements of your data without copying the array.


4. Exercise Review

  1. Setting 0x...114 via int32_t* a:
    • Target is 20 bytes away from 0x...100 ().
    • Since each int32_t is 4 bytes, .
    • Result: a[5] = 37; or *(a + 5) = 37;
  2. Setting 0x...214 via uint8_t* b:
    • Target is 20 bytes away from 0x...200.
    • Since each uint8_t is 1 byte, .
    • Result: b[20] = 37; or *(b + 20) = 37;

Module Navigation