Definition
📌 Pointers
A pointer is a variable that stores the memory address of another variable, instead of a value directly.
Syntax
syntax
type *pointerName;
pointerName = &variable;
*pointerName; // dereference: get the valueExample
c
#include <stdio.h>
int main() {
int age = 25;
int *ptr = &age;
printf("Value: %d\n", age);
printf("Address: %p\n", (void*)&age);
printf("Via pointer: %d\n", *ptr);
return 0;
}Output
outputValue: 25
Address: 0x7ffee2a3c9ac
Via pointer: 25
Important Points
&means "address of";*(on a declaration) means "pointer to", and (on a pointer variable) means "dereference".- A pointer's type must match the type of variable it points to.
- Pointers make it possible to modify a caller's variable from inside a function (pass by reference).
- An uninitialized pointer holds a garbage address — dereferencing it is dangerous.
Common Mistakes
- Dereferencing an uninitialized or
NULLpointer, which crashes the program. - Confusing
*in a declaration ("pointer to") with*in an expression ("dereference"). - Forgetting that array names already act like pointers — writing
&arrwhenarralone is often what you want.
Interview Tip
🎯 Before your interview
Be ready to explain pointer arithmetic: incrementing a pointer moves it forward by
sizeof(type) bytes, not just 1 byte.
