Loading...

C essentialsPointers & Memory

Pointers basics

15 min lesson

Next
  • A pointer is a variable that stores the memory address of another value, not the value itself.
  • Pointers matter because they let you share data between functions, build data structures like lists and trees, and manage memory directly.
  • Think of a pointer as a sticky note with a house address written on it. The note is not the house, but it tells you exactly where to find it.
  • An address is the number that identifies a byte in memory; every variable has one.
  • The & operator gives you the address of a variable: &age.
  • The * operator, called dereference, reads or writes the value at an address: *p.
  • The type of a pointer says what kind of value lives at that address: int *p means p points to an int.
  • A pointer declaration looks like int *p; and should be initialized, for example int *p = &age;.
  • The computer stores the address as a number (often 8 bytes on modern systems) and uses the type to know how many bytes to read or write when you dereference.
  • A realistic use: a function that needs to modify the caller's variable, or a linked list node that points to the next node.
  • A surprising detail: a pointer that has not been initialized points to a random place; dereferencing it is undefined behavior and often crashes.
  • The example program stores a number, takes its address, prints the address, reads the value through the pointer, and changes the value through the pointer.
Keep in mind
  • A pointer holds an address; the type tells it what lives there.
  • Use & to get an address and * to read or write the value at that address.
  • Always initialize pointers, or set them to NULL when they point to nothing.
  • Never dereference a NULL or uninitialized pointer.
  • Pointers are the heart of C. Take your time; you are doing great!
main.c
#include <stdio.h>

int main(void) {
    // A normal int variable with a value
    int age = 30;
    // A pointer that stores the address of age
    int *p = &age;

    // Print the value of age directly
    printf("age: %d\n", age);
    // Print the address stored in p using %p
    printf("address in p: %p\n", (void *)p);
    // Read the value at that address with *
    printf("value via *p: %d\n", *p);

    // Change age through the pointer
    *p = 31;
    // Print the new value of age to prove it changed
    printf("age after change: %d\n", age);

    return 0;
}

What happens: age: 30 address in p: 0x7ffd5e8a9b4c value via *p: 30 age after change: 31

Breaking it down

Normal variable

int age = 30;

Creates an int in memory and stores 30. The computer knows where it lives, but you usually do not need to.

Pointer declaration

int *p = &age;

Declares a pointer to int and stores the address of age in it. The & gives the address, and * in the type means 'pointer to'.

Printing an address

printf("%p", (void *)p);

%p prints a pointer value. The cast to void * is the correct way to pass any pointer to %p.

Dereferencing

*p

Reads the value at the address in p. Since p points to age, *p is 30.

Writing through a pointer

*p = 31;

Stores 31 at the address in p, which is age. Afterward, age becomes 31.

Pointer operators

OperatorNamePurposeExample
&Address-ofGet a variable's addressint *p = &age;
*DereferenceRead/write value at address*p = 31;
%pFormat specifierPrint a pointerprintf("%p", (void *)p)
NULLNull pointerPoint to nothingint *p = NULL;

A helpful analogy

A pointer is like a sticky note with a house address. The note is small and easy to pass around, but it is not the house. When you want to visit, you read the address and go there. If the address is wrong or blank, you end up nowhere useful, which is what happens with an uninitialized or NULL pointer.

Nesting rules

  • 1.Initialize every pointer before use, either to a valid address or to NULL.
  • 2.The pointer type must match the type it points to; int * should point to an int.
  • 3.Use %p with a (void *) cast to print a pointer portably.
  • 4.Never dereference a NULL or uninitialized pointer; check for NULL before *.

Common mistakes

Using an uninitialized pointer

Avoid

int *p; *p = 5; // undefined

Use instead

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

p holds a random address. Writing through it corrupts unknown memory or crashes. Always initialize pointers.

Confusing * in a declaration with dereference

Avoid

int *p = 5; // sets p to address 5

Use instead

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

In a declaration, * means 'pointer to'. You must store an address, not a plain value.

Dereferencing NULL

Avoid

int *p = NULL; printf("%d", *p); // crash

Use instead

if (p != NULL) printf("%d", *p);

NULL points to nothing. Dereferencing it is undefined behavior and usually crashes the program.

Quick tips

  • •Read int *p as 'p is a pointer to int', not 'p is an int'.
  • •Set pointers to NULL when they do not point anywhere yet.
  • •Print addresses with %p and a (void *) cast to see them safely.
  • •Try changing age through *p in the example and watch the printed age change.
  • •Use -Wall -Wextra; the compiler warns about many pointer mistakes.
Previous
Next