Pointer Basics — Address, Dereference & the & Operator
A pointer is a variable that stores a memory **address**. It does NOT store a value directly. The **type** of a pointer (`int*`, `char*`, `double*`) tells the compiler how many bytes to read/write when you dereference it and how much to add when you do pointer arithmetic. Every pointer has a type. `NULL` (address 0) is the canonical 'points to nothing' value.
Pointer Declaration, & and * Operators
#include <stdio.h>
int main(void) {
/* ── Pointer declaration ─────────────────────────────────
int *p — p is a pointer to int
The * in a DECLARATION means "pointer to", not dereference. */
int x = 42;
int *px = &x; /* & = address-of operator: gives address of x */
double d = 3.14;
double *pd = &d;
/* ── Read the address and the value ─────────────────── */
printf("x = %d
", x); /* 42 — the value */
printf("&x = %p
", (void *)&x); /* address of x, e.g. 0x7ffc1234 */
printf("px = %p
", (void *)px); /* same as &x */
printf("*px = %d
", *px); /* 42 — dereference: read the value
* in a EXPRESSION = "value at" */
/* ── Modify via pointer ──────────────────────────────── */
*px = 100; /* write 100 to the memory location px points to */
printf("x after *px=100: %d
", x); /* 100 — x IS changed */
/* ── NULL pointer — the 'points to nothing' sentinel ─── */
int *nullp = NULL; /* 0 — do NOT dereference this! */
if (nullp == NULL) printf("nullp is NULL — safe, don't dereference
");
/* *nullp = 5; ← undefined behaviour, typically SIGSEGV crash */
/* ── sizeof(pointer) — pointer is always the same size ───
On 64-bit systems: every pointer is 8 bytes, regardless of type.
sizeof(int*) == sizeof(char*) == sizeof(double*) == 8 bytes.
The TYPE determines how many bytes are read at that address,
NOT the size of the pointer itself. */
printf("sizeof(int*) = %zu
", sizeof(int *)); /* 8 on 64-bit */
printf("sizeof(double*) = %zu
", sizeof(double *)); /* 8 on 64-bit */
printf("sizeof(char*) = %zu
", sizeof(char *)); /* 8 on 64-bit */
/* ── Pointer to different types — same address, different read */
char bytes[4] = {0x01, 0x00, 0x00, 0x00};
int *iptr = (int *)bytes; /* interpret 4 bytes as int */
printf("int at bytes = %d
", *iptr); /* 1 on little-endian */
return 0;
}Common Mistakes
- Confusing `*` in declaration vs expression — `int *p` declares a pointer-to-int. `*p` in an expression dereferences the pointer (reads the value). These are two entirely different uses of `*`.
- Declaring multiple pointers on one line — `int* p, q;` declares `p` as `int*` but `q` as plain `int`. Write `int *p, *q;` or declare on separate lines to avoid confusion.
- Dereferencing without initialization — `int *p; *p = 5;` — `p` is uninitialised; it contains a garbage address. Writing to it is undefined behaviour and will likely crash or corrupt memory.
Tip
Tip
Practice Pointer Basics Address Dereference the Operator in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Understanding memory layout is essential for efficient C programming
Practice Task
Note
Practice Task — (1) Write a working example of Pointer Basics Address Dereference the Operator from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Pointer Basics Address Dereference the Operator is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- A pointer is a variable that stores a memory **address**.
- Confusing `*` in declaration vs expression — `int *p` declares a pointer-to-int. `*p` in an expression dereferences the pointer (reads the value). These are two entirely different uses of `*`.
- Declaring multiple pointers on one line — `int* p, q;` declares `p` as `int*` but `q` as plain `int`. Write `int *p, *q;` or declare on separate lines to avoid confusion.
- Dereferencing without initialization — `int *p; *p = 5;` — `p` is uninitialised; it contains a garbage address. Writing to it is undefined behaviour and will likely crash or corrupt memory.