C Language Pointers and Arrays: A Pointer Traverses the Entire Array (It’s Not Mystical)

⭐C Language Pointers and Arrays: A Pointer Traverses the Entire Array (It’s Not Mystical)

Author: IoT Smart Academy

By now, you should know the following:

  • <span>&a</span> is the house number (address)
  • A pointer is a variable that holds the house number: <span>int *p</span>
  • <span>*p</span> is used to view/change the value at the address
  • <span>scanf("%d", &a)</span> relies on this logic
  • Using <span>swap(&x, &y)</span> can truly swap values

The biggest problem now is: whenever “pointers and arrays”, “*(p+i)”, or “strings are also arrays” are mentioned, many people get confused.

This article aims to do one thing:

Explain “pointers + arrays” to the point where you think: Is that it? Seems quite natural.

1. The array name is actually the “address of the first element”

First, let’s look at something we are most familiar with:

int a[5] = {10, 20, 30, 40, 50};

There are three statements; please remember them directly:

  1. <span>a</span> in expressions generally represents: <span>&a[0]</span>
  2. <span>a[i]</span> is actually equivalent to: <span>*(a + i)</span>
  3. Therefore: The array name is essentially a “pointer constant pointing to the first element”

But don’t be intimidated by the term “constant”; let’s illustrate with an example:

#include &lt;stdio.h&gt;

int main() {
    int a[5] = {10, 20, 30, 40, 50};

    printf("%p\n", (void*)a);       // value of a (address of the first element)
    printf("%p\n", (void*)&amp;a[0]);   // address of the 0th element

    return 0;
}

You will see that these two addresses are the same (the printing format has requirements, so we use <span>(void*)</span> as the standard practice).

<span>a</span> is not a “copy of the entire array”; it is more like a “fixed pointer to the first element”.

Next, you will be able to understand various “pointer traversing arrays” syntax.

2. Using pointers to traverse arrays: different syntax, same essence

First, let’s use the most common syntax to traverse the array:

#include &lt;stdio.h&gt;

int main() {
    int a[5] = {10, 20, 30, 40, 50};

    for (int i = 0; i &lt; 5; i++) {
        printf("%d ", a[i]);
    }
    return 0;
}

You are already quite familiar with this.

Switching to the “pointer walking” syntax

#include &lt;stdio.h&gt;

int main() {
    int a[5] = {10, 20, 30, 40, 50};
    int *p = a;  // p points to a[0]

    for (int i = 0; i &lt; 5; i++) {
        printf("%d ", *(p + i));
    }
    return 0;
}

Explanation (in layman’s terms):

  • <span>p = a;</span>: means “p remembers the address of a[0]”
  • <span>p + i</span>: moves forward by i elements
  • <span>*(p + i)</span>: looks at the value at that position, which is a[i]

Writing it even shorter (more “pointer-like”)

#include &lt;stdio.h&gt;

int main() {
    int a[5] = {10, 20, 30, 40, 50};
    int *p = a;

    for (; p &lt; a + 5; p++) {   // start from a, stop at a+5
        printf("%d ", *p);     // *p is the current element
    }
    return 0;
}

This is not new magic; it simply replaces “i from 0 to 4” with “the pointer walking from a[0] to just before a[5]”.

The core is not to force them to use this syntax, but to help them: Understand it without fear.

3. Pointers as array parameters: you’ve been doing this all along

Let’s write a function to print an array (normal syntax):

void printArray(int a[], int n) {
    for (int i = 0; i &lt; n; i++) {
        printf("%d ", a[i]);
    }
}

Then call it:

int a[5] = {1,2,3,4,5};
printArray(a, 5);

Many books like to write <span>int a[]</span>, which looks like “passing the array itself”, but essentially it is: passing a pointer (address of the first element).

The following two declarations are equivalent:

void printArray(int a[], int n);
void printArray(int *a, int n);

Passing an array to a function essentially means: “passing the address of the first element, allowing the function to access it using pointers/subscripts.”

So these two lines are perfectly fine:

printArray(a, 5);     // a is &amp;a[0]
void printArray(int *a, int n);  // here we use a pointer to receive it

Once you understand this, the so-called “arrays as parameters passed by pointers” will no longer seem daunting.

4. Strings are also arrays, and using pointers makes it smoother

You must have seen this before:

char name[20];
scanf("%s", name);  // no need for &, because name itself is an address

Now let’s use pointers to traverse the string, which is also easy to understand 👇

#include &lt;stdio.h&gt;

int main() {
    char s[] = "Hello";
    char *p = s;   // points to the first character 'H'

    while (*p != '\0') { // stop at the string terminator
        printf("%c ", *p);
        p++;             // point to the next character
    }
    return 0;
}
  • <span>s</span> is the address of the first character
  • <span>p</span> starts from <span>s</span> and moves forward step by step
  • <span>*p</span> is the current character
  • When encountering <span>'\0'</span><span> (the end marker), it indicates the end of the string</span>

This is the core idea behind all “string processing functions” (<span>strlen</span>, <span>strcpy</span>, etc.): Using pointers to scan linearly.

“It turns out that pointers paired with arrays make handling a series of connected data particularly natural.”

5. Two easily confusing points, clarified in advance

1ïļâƒĢ What is the difference between <span>a</span> and <span>&a</span>?

For beginners, you can simply say:

  • <span>a</span>: commonly used as “the address of the first element” (type is <span>int *</span> if it is <span>int a[]</span>)
  • <span>&a</span>: is “the address of the entire array” (type is <span>int (*)[N]</span>, don’t expand, just know it’s different)

In most beginner codes, we only need to use <span>a</span>. Tell students: Just remember to use a, as long as you don’t write <span>&a</span> to pass to places that require <span>int *</span>.

For example:

void f(int *p);

int a[5];
f(a);    // ✅ pass the address of the first element
// f(&amp;a); // ⚠ïļ type mismatch, don't do this yet

There’s no need to delve into the type system; just touch on it.

2ïļâƒĢ Be cautious of two things when pointers traverse arrays

  1. Out of bounds:
for (p = a; p &lt;= a + 5; p++) // ❌ moved one step too far, accessing outside the array

It should be:

for (p = a; p &lt; a + 5; p++)  // ✅ stop at the position just before a+5
  1. Dangling pointers (using uninitialized pointers):
int *p;
*p = 10;      // ❌ p has not pointed to a valid location

You must first:

int a;
int *p = &amp;a;
*p = 10;      // ✅

“Pointers are like delivery slips; they must clearly state the address. If you stuff something in without an address = dangling pointer writing to random memory = program crash.”

6. A small exercise: use pointers to find the maximum value in an array

Problem

Write a function to find the maximum value in an array using pointers:

int maxInArray(int *a, int n);

Reference code

#include &lt;stdio.h&gt;

int maxInArray(int *a, int n) {
    int max = a[0];        // or *a
    for (int i = 1; i &lt; n; i++) {
        if (a[i] &gt; max) {
            max = a[i];
        }
    }
    return max;
}

int main() {
    int data[6] = {12, 5, 88, 32, 7, 50};
    int max = maxInArray(data, 6);  // data is the starting address
    printf("The maximum value is: %d\n", max);
    return 0;
}

You can also change it to the “pointer walking” version:

int maxInArray(int *a, int n) {
    int max = *a;
    int *p = a + 1;
    int *end = a + n;

    for (; p &lt; end; p++) {
        if (*p &gt; max) {
            max = *p;
        }
    }
    return max;
}

Both versions are correct; the goal is just to help students get used to:

  • Seeing <span>int *a, int n</span> without panic
  • Knowing that what is passed is the address of the array

7. “Array + Pointer” Pocket Summary Card

  1. The array name <span>a</span> → usually represents <span>&a[0]</span>

  2. <span>a[i]</span> ↔ <span>*(a + i)</span> (subscript and pointer syntax are the same)

  3. Passing an array to a function: <span>func(a, n);</span> → inside the function <span>int *a</span>

  4. Strings are <span>char</span> arrays, and <span>char *p</span> reads until it encounters <span>'\0'</span><code><span> to end</span>

  5. When traversing arrays with pointers:

  • Starting point: <span>p = a;</span>
  • Ending point: <span>p < a + n;</span>
  • Current element: <span>*p</span>
  • Do not let uninitialized pointers be misused: first make them point to a real variable/array element.

  • At this point, you are no longer a “student scared of pointers” but someone who can understand others’ array code written with pointers.

    Leave a Comment