Chapter 11: Pointers
Once you master pointers, you will understand how memory really works in C, and it will make learning data structures, dynamic memory, file handling, and even C++ much easier.
Let’s go slowly and carefully – I promise by the end you will love pointers!
1. What are Pointers?
A pointer is a variable that stores the memory address of another variable.
Think of it like this:
- Normal variable: stores the value (like 25 in int age = 25;)
- Pointer variable: stores the address of where that value is kept in memory
Why do we need pointers?
- To work with memory directly
- To pass large data efficiently (without copying)
- To create dynamic memory (malloc, free)
- To work with arrays, strings, functions, linked lists, etc.
2. Declaring Pointers
Syntax:
|
0 1 2 3 4 5 6 |
data_type *pointer_name; |
The * (asterisk) tells the compiler: “This is a pointer.”
Examples:
|
0 1 2 3 4 5 6 7 8 9 |
int *p; // Pointer to an integer float *pricePtr; // Pointer to a float char *namePtr; // Pointer to a character double *piPtr; // Pointer to a double |
How to store an address in a pointer? Use & (address-of operator)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> int main() { int age = 25; int *ptr; ptr = &age; // ptr now holds the memory address of age printf("Value of age: %d\n", age); printf("Address of age: %p\n", &age); // %p for addresses printf("Value stored in ptr: %p\n", ptr); // Same address return 0; } |
Output (example – addresses will be different on your computer):
|
0 1 2 3 4 5 6 7 8 |
Value of age: 25 Address of age: 000000000061FE14 Value stored in ptr: 000000000061FE14 |
3. Dereferencing a Pointer (Getting value from address)
Use * again to get the value stored at the address.
|
0 1 2 3 4 5 6 |
printf("Value at ptr: %d\n", *ptr); // Prints 25 |
Full Example – Changing value using pointer
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> int main() { int x = 10; int *p = &x; printf("Before: x = %d\n", x); *p = 100; // Change value at address stored in p printf("After: x = %d\n", x); // Now x is 100! return 0; } |
Output:
|
0 1 2 3 4 5 6 7 |
Before: x = 10 After: x = 100 |
4. Pointer Arithmetic
You can add or subtract integers from pointers – it moves the pointer by that many elements (not bytes!).
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> int main() { int arr[5] = {10, 20, 30, 40, 50}; int *p = arr; // p points to first element (arr[0]) printf("p points to: %d\n", *p); // 10 p++; // Moves to next integer (arr[1]) printf("After p++: %d\n", *p); // 20 p = p + 2; // Moves two steps forward (arr[3]) printf("After +2: %d\n", *p); // 40 return 0; } |
Important:
- p++ moves 4 bytes (size of int) on 32/64-bit system
- Works with any data type (char moves 1 byte, float moves 4 bytes, etc.)
5. Pointers and Arrays
Array name itself is a pointer to the first element!
|
0 1 2 3 4 5 6 7 |
int arr[5] = {10, 20, 30, 40, 50}; int *p = arr; // Same as: int *p = &arr[0]; |
You can use pointer to access array elements:
|
0 1 2 3 4 5 6 7 8 |
*(p + 0) = 10 // arr[0] *(p + 1) = 20 // arr[1] *(p + 2) = 30 // arr[2] |
Example – Print array using pointer
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { int arr[5] = {100, 200, 300, 400, 500}; int *p = arr; int i; for (i = 0; i < 5; i++) { printf("arr[%d] = %d\n", i, *(p + i)); } return 0; } |
6. Pointers to Functions
You can have a pointer that points to a function!
Syntax:
|
0 1 2 3 4 5 6 |
return_type (*pointer_name)(parameter_types); |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> int add(int a, int b) { return a + b; } int main() { // Pointer to function that takes two ints and returns int int (*funcPtr)(int, int); funcPtr = add; // Point to add function int result = funcPtr(10, 20); // Call function using pointer printf("Result = %d\n", result); return 0; } |
Output:
|
0 1 2 3 4 5 6 |
Result = 30 |
7. Passing Pointers to Functions (Call by Reference)
Normally, when you pass variables to functions, they are copied (call by value) – changes inside function don’t affect original.
With pointers → you pass address → function can change original value (call by reference)
Example – Swap two numbers using pointers
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> void swap(int *x, int *y) { // Receive addresses int temp = *x; *x = *y; *y = temp; } int main() { int a = 10, b = 20; printf("Before swap: a = %d, b = %d\n", a, b); swap(&a, &b); // Pass addresses printf("After swap: a = %d, b = %d\n", a, b); return 0; } |
Output:
|
0 1 2 3 4 5 6 7 |
Before swap: a = 10, b = 20 After swap: a = 20, b = 10 |
Quick Summary Table
| Topic | Key Symbol | Meaning / Use Case |
|---|---|---|
| Declare pointer | * | int *p; |
| Get address | & | p = &x; |
| Get value from address | * | *p gives value of x |
| Pointer to array | p = arr; → p points to arr[0] | |
| Pointer arithmetic | ++, –, +, – | Moves by size of data type |
| Pass by reference | & in call, * in function | Change original values |
| Pointer to function | (*ptr)() | Call functions dynamically |
Today’s Homework
- Write a program that declares an integer, a pointer to it, prints the value and address using both.
- Create a function increment(int *n) that increases the number by 10 using pointer.
- Write a program to reverse an array using pointers (no extra array).
- Create two functions add and multiply. Use a function pointer to call both.
- Write a function swapStrings(char *s1, char *s2) to swap two strings using pointers.
