⚡0 XP🔥0

Pointers

Lesson 10 of 13 ⏱ 6 min read ✎ 3 exercises ❓ 5-question quiz ▶ Runs C

Understand memory addresses, the & and * operators, call by reference, pointer arithmetic, pointers with arrays and strings, and pointer-to-pointer.

Pointers are the most powerful - and most feared - feature of C. The idea is simple once you picture memory.

Memory and addresses#

Every variable lives somewhere in memory, and every memory location has a numeric address. The & operator ("address of") gives you a variable's address:

▶ Try it YourselfC
Ctrl + Enter to run

The addresses change from run to run (the operating system places your program at a random location for security), so there is no fixed output here.

What is a pointer?#

A pointer is a variable that stores an address. Its type says what kind of value lives at that address.

int x = 10;
int *p;      // p is a "pointer to int"
p = &x;      // p now holds the address of x
*p = 50;     // * = "go to the address in p" → this changes x to 50

Two operators:

Operator Name Meaning
&x address-of where x lives
*p dereference / indirection the value stored at the address in p
InteractivePointers in memory - step through the code

Press Next line to run one statement at a time and watch the boxes (variables) and their addresses change.

▶ Try it YourselfC
Ctrl + Enter to run
Output
a = 10
*p = 10 (value at the address in p)
After *p = 50, a = 50
p == &a ? yes
हिंदी में समझें

Pointer एक ऐसा variable है जिसमें किसी दूसरे variable का पता (address) रखा होता है। &a = "a का पता"। *p = "p में जो पता है, वहाँ रखी value"। *p = 50 लिखने से असल में a बदल जाता है, क्योंकि p उसी के घर का पता रखता है।

Call by reference: changing the caller's variables#

Functions receive copies. To let a function change your variables, pass their addresses:

▶ Try it YourselfC
Ctrl + Enter to run
Output
swapWrong: x=3 y=7
swap:      x=7 y=3

This is exactly why scanf("%d", &n) needs & - scanf must write into your variable.

Pointers also let a function "return" several values:

▶ Try it YourselfC
Ctrl + Enter to run
Output
47 / 5 = 9 remainder 2

Pointers and arrays#

An array name used in an expression is the address of its first element: a is the same as &a[0]. Adding to a pointer moves by whole elements:

▶ Try it YourselfC
Ctrl + Enter to run
Output
*p       = 10
*(p + 1) = 20
*(p + 4) = 50
p[2]     = 30
Bytes between a[0] and a[1]: 4
10 20 30 40 50 
Note

a[i] is defined as *(a + i). That is why arrays are passed to functions as pointers: void f(int a[]) and void f(int *a) mean the same thing.

Pointers and strings#

▶ Try it YourselfC
Ctrl + Enter to run
Output
'm' appears 2 times
From index 12: in c

const char *s means "pointer to characters that this function promises not to change".

NULL and dangerous pointers#

  • An uninitialised pointer holds garbage - dereferencing it is undefined behaviour (often a crash).
  • Set pointers you are not using yet to NULL, and check before use.
  • A dangling pointer points to memory that no longer exists (for example, the address of a local variable of a function that has returned).
int *p = NULL;
if (p != NULL) {
    printf("%d", *p);   // safe: only runs if p points somewhere
}

Pointer to pointer#

A pointer can store the address of another pointer:

▶ Try it YourselfC
Ctrl + Enter to run
Output
x = 99, *p = 99, **pp = 99

Pointer-to-pointer appears with 2D dynamic arrays, char *argv[] in main(int argc, char *argv[]), and functions that must change a pointer itself.

Summary#

  • &x gives an address; *p reads/writes the value at an address.
  • Pass addresses to let functions modify variables (call by reference) or return several results.
  • a[i] is *(a + i); pointer arithmetic moves by element size.
  • Initialise pointers; use NULL for "points to nothing" and check before dereferencing.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Swap with pointers

Complete void swap(int *a, int *b) so that main prints After swap: x = 20, y = 10.

Expected output:
After swap: x = 20, y = 10
2

Min and max through pointers

Write void minMax(int a[], int n, int *min, int *max) that stores the smallest and largest values through the pointers. Print Min = 3, Max = 99 style output. Test data is given in main.

Expected output:
Min = 3, Max = 99
3

Sum using pointer arithmetic

Without using a[i] (square brackets), add the elements of the array using a pointer p that moves from the first element to the last. Print Sum = 150.

Expected output:
Sum = 150

Quick Quiz

Q1.

If int x = 5; int *p = &x; - what is *p?

Q2.

int a[5]; int *p = a; - what is *(p + 2)?

Q3.

Why do we use pointers in a swap function?

Q4.

What is a NULL pointer?

Q5.

p++ on an int *p increases the stored address by...