Skip to content

Module 6 · Pointers and Memory — topic 1 of 5

Pointers

A pointer is a variable that holds the address of another object. Learn the & and * operators, pointer types, NULL, and how to read pointer declarations.

Beginner 18 min read·Lesson 22 of 37 in C Programming

Learning objectives

  • Declare pointers and obtain addresses with &
  • Read and write through pointers with *
  • Use NULL and recognise dangling and wild pointers

Definition

A pointer is an object whose value is the address of another object or function. The unary & operator yields the address of its operand; the unary * operator (indirection) accesses the object a pointer points to.

Introduction

Every variable lives at some address in memory — a number identifying where its bytes start. A pointer is simply a variable that stores such an address. With it, code can reach and change data that lives somewhere else.

Pointers are why C can pass large structures to functions without copying them, build linked lists and trees, work with dynamically allocated memory, and talk to hardware at fixed addresses.

Why it matters

Pointers are the single most important — and most feared — C topic. They are required for dynamic memory, strings, arrays in functions, data structures and file handling.

Interviews probe pointer understanding with output questions; real systems fail because of null and dangling pointers. Learning the rules precisely pays off twice.

Detailed explanation

int *p; declares p as a pointer to int. The * belongs to the declarator: int *p, q; makes p a pointer and q a plain int.

p = &x; stores the address of x in p. Now *p is x: reading *p gives x's value and *p = 5; changes x.

The pointer's type tells the compiler what it points to, so *p reads the right number of bytes and pointer arithmetic steps the right distance. void * is a generic pointer that can hold any object address but cannot be dereferenced without a cast.

NULL is a null pointer constant meaning “points to nothing”. Dereferencing a null pointer is undefined behaviour (usually a crash). Always initialise pointers — to a valid address or to NULL — and check before dereferencing when a pointer may be null.

A dangling pointer points to an object that no longer exists (a returned local, freed memory). A wild pointer was never initialised. Both are undefined behaviour to dereference.

ExpressionMeaningValue (example)
xthe int10
&xaddress of x0x7ffd…a4
ppointer value (= &x)0x7ffd…a4
*pobject p points to (= x)10
&paddress of the pointer itself0x7ffd…98

Syntax

C
int x = 10;
int *p = &x; /* p holds the address of x */
*p = 20; /* writes to x through p */
int *q = NULL; /* points to nothing */
if (q != NULL) { *q = 1; }
printf("%p\n", (void *)p); /* print an address */
  • * in a declaration means “pointer to”; in an expression it means “the object pointed to”.
  • Print addresses with %p and a cast to void *.
  • A pointer's own size (often 8 bytes) is independent of what it points to.

Code examples

Address-of and dereference

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

Pointer to pointer

C
#include <stdio.h>
int main(void) {
int value = 7;
int *p = &value;
int **pp = &p;
**pp = 99;
printf("%d %d %d\n", value, *p, **pp);
return 0;
}
Output
99 99 99

More examples

Pointers re-pointed and checked

C
#include <stdio.h>
int main(void) {
int a = 3, b = 8;
int *p = &a;
printf("*p = %d\n", *p);
p = &b; /* re-point to b */
*p += 2; /* b becomes 10 */
printf("a = %d, b = %d\n", a, b);
int *maybe = NULL;
if (maybe)
printf("value %d\n", *maybe);
else
printf("maybe is NULL, not dereferenced\n");
double d = 2.5;
void *any = &d; /* generic pointer */
double *back = any; /* implicit conversion from void * */
printf("through void*: %.1f\n", *back);
printf("sizeof(int*) == sizeof(double*) ? %d\n", sizeof(int *) == sizeof(double *));
return 0;
}
Output
*p = 3
a = 3, b = 10
maybe is NULL, not dereferenced
through void*: 2.5
sizeof(int*) == sizeof(double*) ? 1

Step-by-step explanation

  • p first points to a, so *p is 3.
  • Assigning p = &b changes which object p refers to; a is untouched.
  • *p += 2 updates b from 8 to 10 through the pointer.
  • The null check prevents dereferencing maybe.
  • A void * can hold the address of d; converting back to double * lets us read it. On common platforms all object pointers have the same size.

Expected output

Running “Pointers re-pointed and checked” prints exactly:

TextOutput
*p = 3
a = 3, b = 10
maybe is NULL, not dereferenced
through void*: 2.5
sizeof(int*) == sizeof(double*) ? 1

Practical applications

  • Linked lists, trees and graphs link nodes with pointers.
  • Operating systems pass buffers to drivers as pointers.
  • scanf("%d", &n) works because it receives a pointer to n.

Best practices

  • Initialise every pointer, to a valid address or NULL.
  • Check pointers that may be null before dereferencing.
  • Set pointers to NULL after the object they point to is freed or goes out of scope.

Common mistakes

Dereferencing an uninitialised pointer

Avoid

int *p;
*p = 5;

Prefer

int x;
int *p = &x;
*p = 5;

p holds a garbage address; writing through it corrupts memory or crashes.

Misreading a multi-pointer declaration

Avoid

int* p, q; /* q is NOT a pointer */
q = &x;

Prefer

int *p, *q;
q = &x;

The * applies to each declarator separately. Write one declaration per line or repeat the *.

Important notes

  • C23 introduces nullptr as a typed null pointer constant; NULL remains valid.
  • Comparing or subtracting pointers is only defined for pointers into the same array (or one past its end).

Key takeaways

  • &x gives an address; *p accesses the object at that address.
  • A pointer's type determines what *p reads.
  • Never dereference null, wild or dangling pointers.

Try it yourself

  1. 1
    Write code that uses a pointer to read an integer with scanf and doubles it through the pointer.
  2. 2
    Declare int x = 5; int *p = &x; int **q = &p; and print x through p and through q.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

9 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
  • BeginnerIn `int *p, q;`, what is the type of q?
  • BeginnerWhat does the unary `&` operator produce?
Practise this topic