C - Pointer in C
1. What is a Pointer?
A pointer in C is a variable that stores the memory address of another variable.
Normally:
-
An
intvariable stores an integer. -
A
charvariable stores a character. -
A
floatvariable stores a floating-point number.
But a pointer stores where in memory another value is located.
Think of it like:
A variable is a house with a number inside.
A pointer is a note saying, “The number you want is in house number 1234.”
2. Pointer Declaration Syntax
You declare a pointer by putting an asterisk * before its name:
int *p; // p is a pointer to an int
char *c; // c is a pointer to a char
float *f; // f is a pointer to a float
The * in the declaration means:
“This variable will hold the address of a value of this type.”
3. Getting the Address of a Variable
To get the memory address of a variable, use the address-of operator &:
int x = 10;
printf("%p", &x); // prints the address of x
4. Assigning an Address to a Pointer
int x = 10;
int *p = &x; // p now stores the address of x
Now:
-
pcontains the address ofx. -
*pmeans “go to that address and get the value.”
5. Dereferencing a Pointer
Dereferencing means accessing the value stored at the address a pointer is holding:
printf("%d", *p); // prints 10
Here, *p reads as “the value at the address stored in p”.
6. Pointers and Memory Diagram
Example:
int x = 10;
int *p = &x;
Memory might look like this:
| Variable | Address | Value |
|---|---|---|
x |
0x1000 | 10 |
p |
0x2000 | 0x1000 |
-
pcontains0x1000, which is wherexis stored. -
*paccesses value10stored at0x1000.
7. Changing Values via Pointers
*p = 20; // changes the value of x to 20
printf("%d", x); // prints 20
Because p points to x, dereferencing it lets you modify x directly.
8. Pointer Types Must Match
If p is an int *, it must point to an int:
int a = 5;
int *p = &a; // OK
float f = 3.14;
p = &f; // ERROR: types don’t match
9. Pointer to Pointer (Double Pointer)
A pointer can store the address of another pointer:
int x = 10;
int *p = &x;
int **pp = &p;
printf("%d", **pp); // prints 10
Here:
-
pp→ address ofp -
*pp→ value ofp(address ofx) -
**pp→ value ofx
10. NULL Pointers
If a pointer doesn’t point to anything, set it to NULL:
int *p = NULL;
if (p == NULL) {
printf("Pointer is empty.\n");
}
A NULL pointer is safer than leaving it uninitialized.
11. Pointer Arithmetic
When you increment or decrement a pointer, it moves to the next element of its type.
Example:
int arr[3] = {10, 20, 30};
int *p = arr; // points to arr[0]
p++; // now points to arr[1]
printf("%d", *p); // prints 20
Important: The pointer moves by sizeof(type), not by 1 byte.
12. Common Uses of Pointers
-
Dynamic memory allocation (
malloc,free) -
Arrays and strings (arrays decay to pointers)
-
Function arguments (pass by reference)
-
Pointer to functions (callbacks)
-
Linked data structures (linked lists, trees)
13. Common Mistakes
-
Uninitialized pointers → cause undefined behavior
-
Dangling pointers (pointing to freed memory)
-
Type mismatches
-
Pointer arithmetic errors
-
Forgetting to
freeallocated memory