{ }codelper

Reference › C

C Pointers Explained for Beginners

A pointer is a variable that holds the place where a value lives (its address), not the value itself. &x is "the address of x", and *p is "the value at the place p points to". Once you know these two, everything else is an application of them.

type *p = &variable;   /* p points to variable */
*p = value;            /* store value at the place p points to */

Get an address with & and use that place with *

After int *p = &x;, p holds the address of x. From then on *p behaves like another name for x. Change *p and x changes. Change x and the value you read through *p changes too.

#include <stdio.h> int main(void) {    int x = 10;    int *p = &x;    printf("%d\n", *p);    *p = 20;    printf("%d\n", x);    x += 5;    printf("%d\n", *p);    return 0;}
Output
10
20
25

Edit and run it →

* means different things depending on where you write it. In the declaration int *p it marks "p is a pointer". Everywhere else, *p is "the value at the place it points to" (dereferencing).

Changing the caller's variable from a function

When you pass a variable to a function, its value is copied, so changing it inside the function leaves the caller's variable as it was. If you pass the address, the function can write directly to that place. This is also why you put & in scanf("%d", &n).

#include <stdio.h> void swap(int *a, int *b) {    int t = *a;    *a = *b;    *b = t;} int main(void) {    int x = 1, y = 2;    swap(&x, &y);    printf("%d %d\n", x, y);    return 0;}
Output
2 1

Edit and run it →

Arrays and pointers

When you use an array's name in an expression, it becomes the address of its first element. Adding 1 to a pointer makes it point one element further on, not 1 byte. So a[i] and *(a + i) mean the same thing, and you can write p[i] with a pointer too.

#include <stdio.h> int main(void) {    int a[] = {10, 20, 30};    int *p = a;    printf("%d %d\n", *p, *(p + 2));    printf("%d\n", p[1]);    int sum = 0;    for (int *q = a; q < a + 3; q++) {        sum += *q;    }    printf("%d\n", sum);    return 0;}
Output
10 30
20
60

Edit and run it →

q++ takes one step to the next element. a + 3 is the place just past the last element. Use it only for comparing, and never read it with *.

NULL points to nothing

NULL is a value that means "points nowhere". Functions that return a pointer often return NULL when they didn't find anything or they failed. You must not use * on a pointer that is NULL, so check before you use it.

#include <stdio.h> int *find(int *a, int n, int key) {    for (int i = 0; i < n; i++)        if (a[i] == key) return &a[i];    return NULL;} int main(void) {    int a[] = {4, 8, 15};    int *p = find(a, 3, 8);    if (p != NULL) printf("found %d at %d\n", *p, (int)(p - a));    if (find(a, 3, 7) == NULL) printf("not found\n");    return 0;}
Output
found 8 at 1
not found

Edit and run it →

Subtracting two pointers into the same array gives how many elements apart they are. p - a is 1, so 8 is at index 1.

Common mistakes

Using a pointer before deciding where it points

int *p;      /* not set to point anywhere yet */
*p = 5;

→ The garbage value in p is treated as an address, and the code writes there. This is undefined behavior, so the program may crash or another value may be corrupted

Use a pointer only after you have given it the address of a variable to use (int *p = &x;) or memory allocated with malloc. If it has nothing to point to yet, set it to NULL and check before you use it.

Passing values and expecting them to change

#include <stdio.h>

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

int main(void) {
    int x = 1, y = 2;
    swap(x, y);
    printf("%d %d\n", x, y);
    return 0;
}

→ Still 1 2, not 2 1

The a and b inside the function are copies of x and y. Change the parameters to int *a, int *b and pass the addresses with swap(&x, &y).

Trying to make two pointers with int* a, b;

int* a, b;   /* a is a pointer, b is a plain int */

→ b is an int, not a pointer

The * goes with each name, not with the type. Write int *a, *b; or declare one per line.

Returning the address of a local variable

int *make(void) {
    int n = 42;
    return &n;   /* n is gone when the function ends */
}

→ The pointer you get back points to a variable that is already gone. Reading or writing through it is undefined behavior

For a place that has to remain after the function ends, allocate it with malloc, or take the address of a variable the caller created as a parameter and write there.

Practice problems

See also