Introduction to C Language Pointers: Understanding Why scanf Requires &a

⭐Introduction to C Language Pointers: Understanding Why <span>scanf</span> Requires <span>&a</span>

Author: IoT Smart Academy

Let’s not talk about scary terms like “memory”, “addressing”, and “pass by reference” just yet. Let’s start with a familiar phrase:

scanf("%d", &a);

Why do we have to use <span>&a</span>? Why does writing <span>scanf("%d", a);</span> cause the program to crash? Why is it that for strings, <span>scanf("%s", name);</span> does not require <span>&</span>?

This article will clarify these points. Once you understand this, you will have taken your first step into the world of pointers.

1.<span>scanf("%d", &a)</span> is actually doing something very simple

You write:

int a;
scanf("%d", &a);

Let’s explain it in plain language:

“Read an integer from the keyboard and put it into variable a.”

The key point is: “put it into a”.

So how does <span>scanf</span> know “where a is”? — It needs a “location”.

It’s like telling a delivery person: you can’t just say “deliver to Zhang San”, you have to say “deliver to Building 3, Room 401”.

In the program, this “delivery location” information is:

👉 the variable’s address

<span>&a</span> means:

“Get the address of a (the house number).”

So:

scanf("%d", &a);

The real meaning is:

“Hey scanf, this is the address of a, write the number you read into this address.”

What happens if you don’t add <span>&</span>?

scanf("%d", a); // ❌

You are giving it “the value inside a” (some random number), not “where a is”. It’s like telling the delivery person: “Deliver to the place where 89 is”. It sounds absurd, and the program will certainly write to the wrong memory, leading to errors or crashes.

Remember this rule:

<span>scanf</span> wants the “house number (address)”, not “the current number inside the house”.

2. Can addresses be stored? Yes, that’s the job of pointers

In the line with <span>%d</span>, <span>&a</span> is just temporarily given to <span>scanf</span>. Now there’s a question: Can we store addresses for later use?

Of course, that’s what pointer variables are for.

int a = 10;
int *p;      // Declare a variable to hold the address of an int, called p
p = &a;      // Store the address of a into p

Now:

  • <span>p</span> holds “the house number of a”
  • <span>*p</span> means: Go to the place indicated by p and see the value inside.

Here’s a complete code example:

#include <stdio.h>

int main() {
    int a = 10;
    int *p;        // p is a variable that holds addresses

    p = &a;        // p now remembers the location of a

    printf("The value of a is: %d\n", a);
    printf("Using p to see: %d\n", *p);

    *p = 20;       // Change the value at that location through p
    printf("After modification, the value of a is: %d\n", a);

    return 0;
}

Output:

The value of a is: 10
Using p to see: 10
After modification, the value of a is: 20

Don’t worry about memorizing terms, just remember these three sentences:

  1. <span>p = &a;</span>: p remembers where a is.
  2. <span>*p</span>: Go take a look at that place.
  3. <span>*p = 20;</span>: Go to that place and change the number.

So:

Pointer = a variable that stores addresses<span>*pointer</span> = operate on the content at the address pointed to

Just these two operations, don’t overthink it.

3. Looking back at <span>scanf</span>, you suddenly understand

You can now understand it like this:

scanf("%d", &a);

It roughly looks like this (simplified understanding, not the source code):

void my_scanf_int(int *p) {
    // Read a number from the keyboard and store it in *p
}

You pass <span>&a</span>, which is “the address of a”, and inside the function, it modifies a through <span>*p</span>.

<span>scanf("%d", &a);</span> and your own written:

void set_to_100(int *p) {
    *p = 100;
}
set_to_100(&a);

are essentially the same. Both pass the address in, allowing others to modify your variable.

One sentence to connect the students:

From the first day you learned C, you have been using “pointer version functions” (<span>scanf</span>), only no one told you that you have been using the concept of pointers all along.

4. Why does the string <span>scanf("%s", name);</span> not require <span>&</span>? 🤨

If this point is not clarified, students will always feel like they are just memorizing.

First, look at the code:

char name[20];
scanf("%s", name);

Why don’t we use <span>&name</span>?

The key point is:

The array name <span>name</span> itself, in most cases, is “the address of the first element”.

In other words:

name      ≈   &name[0]

So this line:

scanf("%s", name);

actually passes an “address” to <span>scanf</span><span>, which is fine.</span>

If you write:

scanf("%s", &name[0]);  // also correct

It also works because <span>&name[0]</span> is the address of the first character.

So we leave students with a practical memory aid:

  • <span>int a;</span> needs an address: **<span>&a</span>**
  • <span>char name[20];</span> is already an address: **<span>name</span> is enough**

It’s no longer about rote memorization: “int needs &, char does not need &”, but rather understanding:

“Who is already an address does not need to take the address again.”

5. Using the same logic, understand the “pointer version swap” function

First, look at the version without pointers (many books will mislead you):

void swap(int a, int b) { // ❌ modifies copies, nothing changes outside
    int t = a;
    a = b;
    b = t;
}

<span>main</span>:

int x = 3, y = 5;
swap(x, y);
printf("%d %d\n", x, y); // still 3 5

It cannot be modified because you did not give it “the addresses of x and y”.

Now, change it to the same logic as <span>scanf</span><code><span>:</span>

void swap(int *p, int *q) {
    int t = *p;
    *p = *q;
    *q = t;
}

int main() {
    int x = 3, y = 5;
    swap(&x, &y);   // give it the house numbers
    printf("%d %d\n", x, y);  // 5 3
    return 0;
}

Isn’t it very similar to this line:

scanf("%d", &a);

Same logic:

  • Who needs to change you → just give it the address
  • It gets the address → it can use <span>*</span> to modify you

So you can directly tell students:

“Pointers open up the skill set of <span>scanf</span><span> for you to use yourself. You can also write functions that can modify other variables, not just read them."</span>

6. Pocket Version

  1. <span>&a</span>: the address of a (house number)

  2. Pointer variable: used to hold such addresses

    int *p;  // p specifically stores the address of an int
    
  3. <span>p = &a;</span>: p remembers where a is

  4. <span>*p</span>: go take a look/change at the place pointed to by p

  5. <span>scanf("%d", &a);</span>: give a’s address to scanf so it can modify a

  6. <span>char name[20]; scanf("%s", name);</span>

  • Because <span>name</span> itself is an address (<span>&name[0]</span>)
  • To let a function modify your variable:

    • Pass the address: <span>func(&x);</span>
    • Function parameter writes pointer: <span>void func(int *p)</span>
    • Use <span>*p</span><span> to modify</span>

    As long as these few points are clear, pointers will no longer be the “demon chapter”, but rather:

    “Oh, it’s just the same set of skills as <span>scanf</span><span>, I can use it myself."</span>

    Leave a Comment