CBasicsbeginner

What Is a Pointer in C? Simple Examples

Pointers in C for beginners: address vs value, the & and * operators, three short programs with output and common mistakes.

Published
TL;DR – Quick Answer

A pointer in C is a variable that stores the memory address of another variable. You get an address with the & operator and read the value at an address with the * operator. For example, int *p = &age; makes p point to age, and *p gives the value of age.

On This Page

A pointer in C is a variable that stores the address of another variable. A normal variable holds a value, such as 20. A pointer holds a location in memory, such as "the box where 20 is kept".

This page explains address vs value with a simple memory-box picture, then shows three short programs with output. If you are new to C, read our C programming step by step guide first.

Address vs value: the memory-box picture

Think of computer memory as a long row of numbered boxes. Each box has a number written on it (the address) and something kept inside it (the value).

When you write int age = 20;, C picks a box, calls it age and puts 20 inside.

  Name:      age
           +------+
  Value:   |  20  |
           +------+
  Address:  1000

The address 1000 is only for this picture. Real addresses are long numbers, usually shown in hexadecimal, such as 0x7ffd5e8c and they change each time the program runs.

Now make a pointer called p that stores the address of age:

  Name:      p                 age
           +------+          +------+
  Value:   | 1000 | -------> |  20  |
           +------+          +------+
  Address:  2000              1000

p is also a variable, so it has its own box and its own address (2000). The value inside p is 1000, which is the address of age. We say "p points to age".

Expression Meaning Value in the picture
age Value of age 20
&age Address of age 1000
p Value of p (an address) 1000
*p Value at the address stored in p 20
&p Address of p itself 2000

The two pointer operators

Operator Name Read it as Example
& Address-of "address of" &age is the address of age
* Dereference "value at" *p is the value at the address in p

The * symbol has two jobs, which confuses many beginners:

  • In a declaration, int *p; means "p is a pointer to an int".
  • In an expression, *p means "go to the address in p and use the value there".

How to declare a pointer in C

The general form is:

type *name;

Examples:

int *p;       /* pointer to an int */
float *f;     /* pointer to a float */
char *c;      /* pointer to a char */

The type tells C what kind of value lives at the address. An int * should point to an int. This matters when you read the value with * and when you move the pointer, as you will see in program 3.

Pro tip: Always give a pointer a value when you create it. Either point it at a real variable, int *p = &age;, or set it to NULL. An uninitialised pointer holds a random address.

Program 1: print an address and a value

#include <stdio.h>

int main(void) {
    int age = 20;
    int *p = &age;

    printf("Value of age   : %d\n", age);
    printf("Address of age : %p\n", (void *)&age);
    printf("Value of p     : %p\n", (void *)p);
    printf("Value at p     : %d\n", *p);

    *p = 25;   /* change age through the pointer */
    printf("New age        : %d\n", age);

    return 0;
}

Sample output:

Value of age   : 20
Address of age : 0x7ffd5e8c4a2c
Value of p     : 0x7ffd5e8c4a2c
Value at p     : 20
New age        : 25

Your addresses will be different, and they can change every run. What matters is that the second and third lines always match, because p holds the address of age.

The line *p = 25; is the key idea. It means "put 25 in the box that p points to". That box is age, so age becomes 25 even though we never wrote age = 25.

The %p format prints an address. The C standard expects a void * for %p, which is why the program casts with (void *).

Program 2: swap two numbers using pointers

In C, function arguments are passed by value. The function gets a copy, so changing the copy does not change the original. Pointers solve this. You pass the address, and the function changes the value at that address.

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main(void) {
    int x = 5, y = 9;

    printf("Before: x = %d, y = %d\n", x, y);
    swap(&x, &y);
    printf("After : x = %d, y = %d\n", x, y);

    return 0;
}

Output:

Before: x = 5, y = 9
After : x = 9, y = 5

Step by step:

Line a points to b points to temp x y
Start of swap x y - 5 9
temp = *a; x y 5 5 9
*a = *b; x y 5 9 9
*b = temp; x y 5 9 5

If you wrote void swap(int a, int b) without pointers, the swap would happen only inside the function, and x and y in main would stay 5 and 9. This is why scanf("%d", &x) needs the &: scanf must know where to store the number.

Program 3: pointers and arrays

The name of an array, used in most expressions, turns into the address of its first element. So you can point a pointer at an array and move through it.

#include <stdio.h>

int main(void) {
    int marks[4] = {72, 85, 64, 90};
    int *p = marks;   /* same as p = &marks[0]; */
    int i;

    for (i = 0; i < 4; i++) {
        printf("marks[%d] = %d\n", i, *(p + i));
    }

    return 0;
}

Output:

marks[0] = 72
marks[1] = 85
marks[2] = 64
marks[3] = 90

p + 1 does not add 1 byte. It moves to the next int. If an int is 4 bytes on your system, p + 1 is 4 bytes ahead of p. This is called pointer arithmetic, and it is why the pointer type matters.

Expression Same as Value
*p marks[0] 72
*(p + 1) marks[1] 85
*(p + 2) marks[2] 64
*(p + 3) marks[3] 90

In fact, marks[i] is defined as *(marks + i) in C. Array indexing is pointer arithmetic underneath.

NULL pointer

A NULL pointer points to nothing. Use it to say "no address yet" and check it before you use the pointer.

#include <stdio.h>

int main(void) {
    int *p = NULL;

    if (p == NULL) {
        printf("p does not point to anything yet\n");
    } else {
        printf("Value: %d\n", *p);
    }

    return 0;
}

Output:

p does not point to anything yet

Reading *p when p is NULL is undefined behaviour. On most systems the program crashes with a segmentation fault.

Size of a pointer

A pointer stores an address, so its size depends on the system, not on the type it points to. An int * and a char * are usually the same size.

printf("%zu\n", sizeof(int *));
printf("%zu\n", sizeof(char *));

On most 64-bit computers both lines print 8. On a 32-bit system they usually print 4. Use %zu to print a value of type size_t, which is what sizeof gives.

Common pointer mistakes

Mistake Example What goes wrong
Using an uninitialised pointer int *p; *p = 10; p holds a random address, so you write to unknown memory
Dereferencing NULL int *p = NULL; printf("%d", *p); Undefined behaviour, often a crash
Forgetting & in scanf scanf("%d", x); scanf gets a value instead of an address
Mixing up p and *p p = 25; instead of *p = 25; Changes the address, not the value
Returning the address of a local variable return &local; The variable no longer exists after the function ends (dangling pointer)
Going past the array *(p + 4) for a 4-element array Reads memory that does not belong to the array

Common mistake: Writing int* a, b; and thinking both are pointers. Only a is a pointer. b is a plain int. Write int *a, *b; or declare them on separate lines.

Where pointers are used

  • Functions that change caller variables, like swap and scanf.
  • Arrays and strings. A C string is an array of char ending in '\0', and string functions work through char *.
  • Dynamic memory. malloc returns a pointer to memory you request while the program runs.
  • Data structures. Each node in a linked list uses a pointer to reach the next node. Trees and graphs work the same way.
  • Passing large structures. Passing a pointer to a structure avoids copying the whole structure.

Languages such as Java and Python do not give you raw pointers. They use references, which are handled for you. See C vs C++ for how C++ adds references on top of C pointers.

Quick revision

  1. A pointer stores an address.
  2. &x gives the address of x.
  3. *p gives the value at the address in p.
  4. *p = value changes the variable that p points to.
  5. Set pointers to a real address or NULL when you create them.
  6. p + 1 moves by one element, not one byte.

Pointers become easy once you draw the boxes. For each program, draw a box per variable, write its address below it, and draw arrows for pointers. Pointers also connect closely with how functions receive parameters. When you can trace all three programs above without running them, test yourself with the C programming interview questions.

Frequently Asked Questions

What is a pointer in C in simple words?
A pointer is a variable that holds an address instead of a normal value. The address tells you where another variable is stored in memory.
What is the difference between & and * in C?
& gives the address of a variable. * used with a pointer gives the value stored at that address. In a declaration such as int *p, the * only says that p is a pointer.
What is a NULL pointer?
A NULL pointer is a pointer that points to nothing. Set a pointer to NULL when it has no valid address yet, and check for NULL before using it.
What is the size of a pointer in C?
It depends on the system, not on the type it points to. On most 64-bit systems a pointer is 8 bytes, and on 32-bit systems it is usually 4 bytes. Use sizeof to check.
Why do we need pointers in C?
Pointers let a function change variables of the caller, let you work with arrays and strings efficiently, and are needed for dynamic memory with malloc and for structures like linked lists.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 4 December 2026 LinkedIn
Chat with us