Pointers are the most powerful - and most feared - feature of C. The idea is simple once you picture memory.
Memory and addresses#
Every variable lives somewhere in memory, and every memory location has a numeric address. The & operator ("address of") gives you a variable's address:
The addresses change from run to run (the operating system places your program at a random location for security), so there is no fixed output here.
What is a pointer?#
A pointer is a variable that stores an address. Its type says what kind of value lives at that address.
int x = 10;
int *p; // p is a "pointer to int"
p = &x; // p now holds the address of x
*p = 50; // * = "go to the address in p" → this changes x to 50
Two operators:
| Operator | Name | Meaning |
|---|---|---|
&x |
address-of | where x lives |
*p |
dereference / indirection | the value stored at the address in p |
a = 10 *p = 10 (value at the address in p) After *p = 50, a = 50 p == &a ? yes
Pointer एक ऐसा variable है जिसमें किसी दूसरे variable का पता (address) रखा होता है। &a = "a का पता"। *p = "p में जो पता है, वहाँ रखी value"। *p = 50 लिखने से असल में a बदल जाता है, क्योंकि p उसी के घर का पता रखता है।
Call by reference: changing the caller's variables#
Functions receive copies. To let a function change your variables, pass their addresses:
swapWrong: x=3 y=7 swap: x=7 y=3
This is exactly why scanf("%d", &n) needs & - scanf must write into your variable.
Pointers also let a function "return" several values:
47 / 5 = 9 remainder 2
Pointers and arrays#
An array name used in an expression is the address of its first element: a is the same as &a[0]. Adding to a pointer moves by whole elements:
*p = 10 *(p + 1) = 20 *(p + 4) = 50 p[2] = 30 Bytes between a[0] and a[1]: 4 10 20 30 40 50
a[i] is defined as *(a + i). That is why arrays are passed to functions as pointers: void f(int a[]) and void f(int *a) mean the same thing.
Pointers and strings#
'm' appears 2 times From index 12: in c
const char *s means "pointer to characters that this function promises not to change".
NULL and dangerous pointers#
- An uninitialised pointer holds garbage - dereferencing it is undefined behaviour (often a crash).
- Set pointers you are not using yet to
NULL, and check before use. - A dangling pointer points to memory that no longer exists (for example, the address of a local variable of a function that has returned).
int *p = NULL;
if (p != NULL) {
printf("%d", *p); // safe: only runs if p points somewhere
}
Pointer to pointer#
A pointer can store the address of another pointer:
x = 99, *p = 99, **pp = 99
Pointer-to-pointer appears with 2D dynamic arrays, char *argv[] in main(int argc, char *argv[]), and functions that must change a pointer itself.
Summary#
&xgives an address;*preads/writes the value at an address.- Pass addresses to let functions modify variables (call by reference) or return several results.
a[i]is*(a + i); pointer arithmetic moves by element size.- Initialise pointers; use
NULLfor "points to nothing" and check before dereferencing.