Back to C Programming
2026-03-275 min read

14.5 Dereferencing Pointers

Learn 14.5 Dereferencing Pointers step by step with clear examples and exercises.

Title: Dereferencing Pointers in C Programming (Expanded Version)

Why This Matters

Understanding dereferencing pointers is crucial for mastering C programming as it allows you to manipulate memory directly and create dynamic data structures such as linked lists, arrays, and stacks. It also plays a significant role in solving real-world problems, debugging complex code, and preparing for job interviews.

Mastering dereferencing pointers will enable you to:

  • Manipulate memory more efficiently
  • Create dynamic data structures like linked lists, arrays, and stacks
  • Solve real-world problems by managing memory dynamically
  • Debug complex code by understanding how pointers work
  • Prepare for job interviews with a strong foundation in C programming

Prerequisites

Before diving into dereferencing pointers, you should be familiar with the following concepts:

  1. Basic C syntax (operators, control structures, functions)
  2. Variables and data types
  3. Arrays
  4. Pointers basics (declaration, initialization, and accessing memory addresses)
  5. Basic input/output operations using printf() and scanf()
  6. Understanding the concept of memory allocation in C

Core Concept

A pointer is a variable that stores the memory address of another variable. Dereferencing a pointer means accessing or modifying the data stored at the memory location pointed to by the pointer. The dereference operator * is used for this purpose.

int num = 5; // Declare an integer variable named 'num' and initialize it with the value 5
int *ptr; // Declare a pointer to an integer named 'ptr' (no initial value)
ptr = # // Assign the memory address of 'num' to 'ptr' (the address-of operator `&`)

// Dereference 'ptr' to access the value stored at the memory location it points to:
printf("The value of num is %d", *ptr); // Outputs "The value of num is 5"

In this example, we declared a variable named num, created a pointer ptr to an integer, and assigned the address of num to ptr. We then used the dereference operator * to access the value stored at the memory location pointed to by ptr.

Pointer Arithmetic

Pointer arithmetic allows you to perform operations on pointers that move them to adjacent memory locations. For example, if ptr points to an integer and sizeof(int) = 4, incrementing ptr (ptr++) moves it to the next integer's address.

int arr[5] = {1, 2, 3, 4, 5}; // Declare an array 'arr' with 5 integers and initialize them
int *ptr = &arr[0]; // Initialize 'ptr' to point to the first element of 'arr'
printf("The value pointed by ptr is %d\n", *ptr); // Outputs "The value pointed by ptr is 1"
ptr++; // Move 'ptr' to the next integer (address + sizeof(int))
printf("The value pointed by ptr after incrementing is %d\n", *ptr); // Outputs "The value pointed by ptr after incrementing is 2"

Worked Example

Let's create a simple program that demonstrates dereferencing pointers and pointer arithmetic:

#include <stdio.h>

int main() {
int arr[5] = {1, 2, 3, 4, 5}; // Declare an array 'arr' with 5 integers and initialize them
int *ptr; // Declare a pointer to an integer named 'ptr' (no initial value)

ptr = arr; // Initialize 'ptr' to point to the first element of 'arr'
printf("The address of the first element in arr is %p\n", &arr[0]); // Outputs the memory address of the first element in 'arr'

printf("\nValues before traversing:\n");
for (int i = 0; i < 5; ++i) {
printf("arr[%d]: %d\n", i, arr[i]); // Outputs the values of the elements in 'arr'
}

printf("\nValues after traversing using pointer:\n");
for (int i = 0; i < 5; ++i) {
printf("The value pointed by ptr is %d\n", *ptr); // Outputs the values of the elements in 'arr' using pointer 'ptr'
ptr++; // Move 'ptr' to the next integer (address + sizeof(int))
}

return 0;
}

Common Mistakes

  1. Forgetting the dereference operator *: When trying to access or modify the data stored at a memory location pointed to by a pointer, you must use the dereference operator *.
int num = 5;
int *ptr = &num;

// Incorrect:
printf("The value of num is %p", ptr); // Outputs "The value of num is <random memory address>" (not the value)

// Correct:
printf("The value of num is %d", *ptr); // Outputs "The value of num is 5"
  1. Accessing invalid or uninitialized pointers: Always ensure that a pointer points to a valid memory location before dereferencing it, otherwise you may end up accessing random memory or causing undefined behavior.
  1. Confusing the address-of operator & and dereference operator *: The address-of operator & is used to get the memory address of a variable, while the dereference operator * is used to access or modify the data stored at the memory location pointed to by a pointer.

Common Mistakes (Continued)

  1. Pointer aliasing: Assigning different pointers to the same memory location can lead to undefined behavior and errors, as changes made through one pointer may not be reflected in others pointing to the same location.
int num = 5;
int *ptr1 = &num, *ptr2;
ptr2 = &num; // Both ptr1 and ptr2 now point to the same memory address
*ptr1 = 10; // Changing the value through ptr1 will also change the value through ptr2
  1. Forgetting to deallocate dynamically allocated memory: When using dynamic memory allocation functions like malloc(), calloc(), and realloc(), you must remember to free the memory when it's no longer needed to prevent memory leaks.

Practice Questions

  1. Write a program that declares two integer variables and swaps their values using pointers.
  2. Given the following code, what will be the output of the program?
int num = 5;
int *ptr = &num;
printf("%d", ptr);
  1. Write a function that takes an array and its size as input arguments and returns the sum of all elements in the array using pointers.

FAQ

  1. Why do we use pointers for dynamic memory management?

Pointers allow you to dynamically allocate and deallocate memory during runtime, enabling the creation of dynamic data structures such as linked lists, arrays, and stacks. This flexibility is essential for solving many real-world problems efficiently.

  1. How can I check if a pointer is pointing to a valid memory location?

You can use the NULL keyword or 0 to represent an invalid memory address. Before dereferencing a pointer, you should always verify that it is not equal to NULL. Additionally, using bounds checking functions such as malloc(), calloc(), and realloc() can help prevent accessing out-of-bounds memory.

  1. What happens if I try to dereference an uninitialized pointer?

Dereferencing an uninitialized pointer may result in undefined behavior, as the value of the pointer might not point to a valid memory location. This could lead to unexpected results, crashes, or security vulnerabilities. Always ensure that a pointer is properly initialized before using it.