📘 Lesson  ·  Lesson 54

Pointers in C

What is a pointer? (simple idea)

Think of your computer's memory as a long street of houses, and every house has an address. When you create a variable like int x = 10;, the value 10 is stored inside one of those houses. That house has an address too.

A pointer is simply a variable that stores an address instead of a normal value. So while x holds the number 10, a pointer to x holds "the address where 10 lives." That one idea is the whole foundation of pointers.

💡 In one line

A normal variable holds a value. A pointer holds the address of a value.

Declaring and using a pointer

You declare a pointer by putting a * before its name, and you say what type of value it points to.

C Language
#include <stdio.h>
int main() {
    int x = 10;
    int *p = &x;        // p stores the address of x

    printf("Value of x     : %d\n", x);
    printf("Address of x   : %p\n", &x);
    printf("p holds address: %p\n", p);
    printf("Value via p    : %d\n", *p);   // dereference
    return 0;
}
Output:
Value of x : 10
Address of x : 0x7ffee3b2c8
p holds address: 0x7ffee3b2c8
Value via p : 10

Notice that &x and p print the same address — because p is literally holding the address of x. And *p gives back 10, the value at that address. (Your exact address numbers will differ each run — that is normal.)

The two key operators: & and *

Everything with pointers comes down to these two operators, which are opposites:

OperatorNameMeaning
&Address-of&x = the address where x is stored
*Dereference*p = the value stored at the address in p
💡 Easy way to remember

& asks "where is it?" and * asks "what is there?"

Changing a value through a pointer

Because a pointer knows the address of a variable, it can also change that variable's value directly. This is one of the most powerful things pointers do.

C Language
#include <stdio.h>
int main() {
    int x = 10;
    int *p = &x;

    *p = 25;            // change x through the pointer
    printf("x is now %d\n", x);
    return 0;
}
Output:
x is now 25

We never wrote x = 25 directly, yet x became 25 — because *p reaches into x's memory and changes it. This is exactly how functions modify variables passed to them (see call by value vs reference).

NULL pointers and safety

A pointer that does not yet point to anything valid should be set to NULL. This gives you a safe, checkable "empty" state.

C Language
#include <stdio.h>
int main() {
    int *p = NULL;      // points to nothing yet

    if (p != NULL)
        printf("Value: %d\n", *p);
    else
        printf("Pointer is empty, not safe to use.\n");
    return 0;
}
Output:
Pointer is empty, not safe to use.
⚠️ Golden rule

Never use *p on a pointer that is NULL or was never given a valid address — it crashes the program (a "segmentation fault"). Always check first.

Why do we even need pointers?

Pointers feel abstract at first, but C depends on them for real work:

  • Changing variables inside functions — a function can update your original variable, not just a copy.
  • Arrays and strings — these are handled through addresses under the hood.
  • Dynamic memory — asking for memory at runtime with malloc gives you a pointer.
  • Efficiency — passing an address is cheaper than copying large data.

Common mistakes

  • Using *p before p points to a valid address (uninitialised pointer).
  • Confusing & and * — remember: & gives an address, * reads a value.
  • Dereferencing a NULL pointer without checking it first.
  • Forgetting that the pointer's type must match the variable's type (an int * for an int).
🏋️ Practice

Declare two integers a and b, make a pointer point to each, and swap their values using only the pointers (*pa and *pb). Print the results to confirm the swap worked.

Summary

  • A pointer stores the address of another variable, not a plain value.
  • &x gives an address; *p reads (or writes) the value at that address.
  • Through *p you can change the original variable directly.
  • Set unused pointers to NULL and always check before dereferencing.
  • Pointers power functions, arrays, strings and dynamic memory in C.

Frequently Asked Questions

What exactly is a pointer in C?
A pointer is a variable that stores the memory address of another variable, instead of a normal value like a number. If a variable lives at a certain place in memory, a pointer lets you hold and use that place's address.
What is the difference between & and * in C?
The & operator means "address of" — &x gives you where x is stored. The * operator means "value at" (dereference) — *p gives you the value kept at the address inside pointer p. They are opposites of each other.
What is a NULL pointer and why is it useful?
A NULL pointer is a pointer set to NULL, meaning it points to nothing valid. It is useful as a clear "empty" state you can check before using the pointer, which prevents you from accidentally reading a random memory address.
Why do pointers crash programs so often?
Most pointer crashes come from dereferencing a pointer that does not point to valid memory — an uninitialised pointer, a NULL pointer, or a pointer to memory that was already freed. Always make sure a pointer points somewhere valid before using * on it.
Do I really need pointers as a beginner?
Yes, eventually. Pointers are essential for changing a variable inside a function, working with arrays and strings efficiently, and allocating memory at runtime with malloc. You can start without them, but core C topics quickly depend on understanding pointers.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.