Pointers in C: Fundamentals & Memory Addresses
The core superpower of C: understanding memory addresses, the address-of operator (&), dereference operator (*), and pointer arithmetic.
- A pointer is a variable whose value is the physical or virtual memory address of another variable.
- The address-of operator
&retrieves the memory address of an operand. - The dereference / indirection operator
*accesses the value stored at the address pointed to. - Pointer arithmetic is scaled automatically by the compiler based on the data type's byte size.
- Always initialize pointers; uninitialized pointers (wild pointers) can corrupt random memory.
Anatomy of a Pointer
Every variable occupies a location in RAM. A pointer stores that memory address:
int *ptrdeclares a pointer to an integer.&xyields the address ofx.*ptr(dereferencing) accesses or changes the value insidex.
When used in a declaration (`int *ptr`), the asterisk denotes that `ptr` is a pointer variable. When used in an expression (`*ptr = 99`), it dereferences the pointer to access the target memory value.
Pointer Arithmetic & Array Traversal
When you increment a pointer (ptr++), it does NOT simply add 1 to the numeric address. It advances by sizeof(type) bytes! If an int is 4 bytes and ptr points to address 1000, then ptr + 1 evaluates to 1004.
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 2 correct(0 attempted)
What does the dereference operator (*) do when used on a valid pointer?
IntermediateIf double *p points to address 2000, and sizeof(double) is 8 bytes, what is p + 3?
Intermediate