Pointers in C
Introduction to Pointers
A pointer is a variable that stores the memory address of another variable. Instead of holding a data
value directly, a pointer "points" to the location where a value is stored. This is a powerful feature in
C programming as it allows for efficient manipulation of memory and data structures.
Declaration and Initialization of Pointers
To declare a pointer, use the * operator along with the data type:
int *ptr; // Pointer to an integer
To initialize a pointer, use the address-of operator &:
int num = 10;
int *ptr = # // ptr now holds the address of num
Basic Pointer Operations
Pointers can be used to:
- Access the value of the variable it points to (dereferencing)
- Modify the value at a specific memory address
- Perform arithmetic operations on addresses
Example 1: Basic Pointer Declaration and Dereferencing
#include <stdio.h>
int main() {
int num = 20;
int *ptr = # // Pointer to num
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value at ptr (dereferencing): %d\n", *ptr);
return 0;
Pointer Arithmetic
Pointer arithmetic is a way to move through arrays and memory. When adding or subtracting to a
pointer, it moves in steps of the data type it points to.
Example 2: Pointer Arithmetic with Integer Array
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *ptr);
ptr++; // Move to the next element
return 0;
Pointers and Arrays
An array name itself acts like a pointer to the first element, making array elements accessible via
pointer arithmetic.
Example 3: Accessing Array Elements Using Pointers
#include <stdio.h>
int main() {
int arr[] = {5, 10, 15, 20};
int *ptr = arr;
for (int i = 0; i < 4; i++) {
printf("Element %d: %d\n", i, *(ptr + i));
return 0;
Pointers and Functions
Pointers are often used in functions for:
1. Passing data by reference (e.g., for modifying values)
2. Efficiently passing large data structures like arrays
Example 4: Swapping Two Numbers Using Pointers
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
int main() {
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
Pointers to Pointers
A pointer to a pointer is used for handling multidimensional arrays and memory management in
dynamic data structures.
Example 5: Pointer to Pointer
#include <stdio.h>
int main() {
int num = 30;
int *ptr = #
int **pptr = &ptr;
printf("Value of num: %d\n", num);
printf("Value using *ptr: %d\n", *ptr);
printf("Value using **pptr: %d\n", **pptr);
return 0;
Dynamic Memory Allocation Using Pointers
Dynamic memory allocation functions like malloc, calloc, and free in C make use of pointers for
managing memory at runtime.
Example 6: Dynamic Memory Allocation with malloc and free
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5;
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 1;
for (int i = 0; i < n; i++) {
ptr[i] = i + 1;
printf("Value at ptr[%d]: %d\n", i, ptr[i]);
free(ptr);
return 0;
Advanced Examples
Example 7: Pointer to an Array of Pointers (2D Array)
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 3, cols = 4;
int **matrix = (int **)malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = i * cols + j;
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
printf("\n");
for (int i = 0; i < rows; i++) {
free(matrix[i]);
free(matrix);
return 0;
Conclusion
Pointers in C provide a powerful way to access and manipulate memory. Mastering pointers is
crucial for effective C programming, especially for dynamic memory allocation, data structures, and
system-level programming.