Skip to content
CurriculumChapter 19 ยท Pointers & Memory
IntermediateHigh focus~16 min

Pointers, Address & Dereference

A pointer is just an address with a type. That type tells the compiler how many bytes to read and how far to move when you add one.

In plain English

A pointer is a street address. The type tells you what kind of building is at that address.

01

Addresses, declaration, and dereference

The & operator yields the address of an object. A pointer variable stores that address. The * operator dereferences a pointer, giving access to the object it points to. The type matters: int *p means p points to an int, so *p reads sizeof(int) bytes. An uninitialized pointer contains an indeterminate address; dereferencing it is undefined behaviour.

chapter-19.cC
int x = 42;
int *p = &x;      /* p stores the address of x */

printf("%d\n", x);    /* 42 */
printf("%d\n", *p);   /* 42, same object */
*p = 99;              /* writes through the pointer */
printf("%d\n", x);    /* 99 */
02

Pointer types and NULL

A pointer to int is a different type from a pointer to char, even if both hold addresses. The type controls dereference size and pointer arithmetic. A null pointer points to nothing and must not be dereferenced. Use NULL for clarity and check a pointer before dereferencing it when failure is possible.

chapter-19.cC
int *ip = NULL;
char *cp = NULL;

if (ip != NULL) {
    printf("%d\n", *ip);
}

Watch it happen

Step through the memory changes one instruction at a time.

step-by-step

Step 1 of 6

Create x

int x = 42; stores 42 at address 0x7ffc1000.

ppint **
0x7ffc1008
uninitialized
pint *
0x7ffc1004
uninitialized
xint
0x7ffc1000
42

Key takeaways

  • A pointer stores an address; its type controls dereference size.
  • & takes an address, * dereferences a pointer.
  • Uninitialized pointers must not be dereferenced.
  • NULL represents a pointer that points to nothing.

Common pitfalls

  • Dereferencing an uninitialized or null pointer.
  • Confusing the pointer value with the value it points to.
  • Assigning an int to a pointer without a cast.

Key focus

  • Trace pointer values and dereferenced values step by step.
  • Know the meaning of & and * in declarations and expressions.

Quick check

0 / 2 answered

1. What does *p do?

2. What is the value of &x?