Back to C Programming
2026-07-145 min read

Example: Working of Pointers (C Programming)

Learn Example: Working of Pointers (C Programming) step by step with clear examples and exercises.

Why This Matters

Pointers are a fundamental concept in C programming that allow you to manipulate memory directly, which is crucial for understanding low-level programming concepts and optimizing performance. In this lesson, we'll explore how pointers work in C programming, with practical examples and common mistakes to avoid.

Prerequisites

Before diving into pointers, it is essential to have a solid understanding of the following topics:

  • Basic C syntax (variables, data types, operators)
  • Arrays and their memory representation
  • Addressing in C programming (& operator)

Core Concept

What are Pointers?

In C programming, a pointer is a variable that stores the memory address of another variable. By using pointers, we can manipulate the values stored at specific memory locations directly.

int num = 10; // Declaring an integer variable 'num' and initializing it with value 10
int *ptr; // Declaring a pointer 'ptr' that can store the address of an integer variable
ptr = # // Assigning the address of 'num' to the pointer 'ptr'

In this example, we have declared an integer variable num and a pointer ptr. We then assign the memory address of num to ptr using the address-of operator (&).

Pointer Notation

To access the value stored at the memory location pointed by a pointer, we use the indirection operator (*). The general syntax for accessing the value is:

*pointer_name

For example:

printf("The value of num is: %d\n", *ptr); // Outputs: 10

Pointer Arithmetic

Pointer arithmetic allows us to manipulate the memory location pointed by a pointer. We can increment or decrement a pointer to point to the next or previous memory location of the same data type. Here's an example:

int arr[] = {1, 2, 3, 4, 5}; // Declaring an array 'arr' with 5 integer elements
int *ptr = &arr[0]; // Assigning the address of the first element to the pointer 'ptr'
printf("The value of ptr is: %p\n", ptr); // Outputs the memory address of arr[0]

ptr++; // Incrementing the pointer to point to arr[1]
printf("The value of *ptr is: %d\n", *ptr); // Outputs: 2

Pointer and Arrays Relationship

Arrays in C are simply contiguous blocks of memory allocated for a specific data type. When we declare an array, the compiler automatically calculates its base address (the starting memory location) and assigns it to the first element's address. This is why we can use pointer arithmetic with arrays:

int arr[] = {1, 2, 3, 4, 5}; // Declaring an array 'arr' with 5 integer elements
int *ptr = &arr[0]; // Assigning the base address of 'arr' to the pointer 'ptr'
printf("The value of ptr is: %p\n", ptr); // Outputs the memory address of arr[0]

ptr++; // Incrementing the pointer to point to arr[1]
printf("The value of *ptr is: %d\n", *ptr); // Outputs: 2

Passing Arrays and Pointers to Functions

When we pass an array to a function, it is treated as a pointer to the first element. This means that functions can manipulate the elements of an array by using pointers. Here's an example:

void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5}; // Declaring an array 'arr' with 5 integer elements
printArray(arr, sizeof(arr) / sizeof(arr[0])); // Calling the function with 'arr' and its size

return 0;
}

Worked Example

Let's write a program that takes an array of integers as input, finds the sum of all even numbers, and prints the result.

#include <stdio.h>

void findSumEven(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
sum += arr[i];
}
}
printf("The sum of even numbers is: %d\n", sum);
}

int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // Declaring an array 'arr' with 9 integer elements
findSumEven(arr, sizeof(arr) / sizeof(arr[0])); // Calling the function with 'arr' and its size

return 0;
}

In this example, we define a function findSumEven() that takes an array of integers and its size as input. Inside the function, we use a loop to iterate through each element in the array. If the current element is even, we add it to the sum variable. After processing all elements, we print the sum of even numbers.

Common Mistakes

  1. Forgetting the indirection operator (*) when accessing a pointer's value:
int num = 10;
int *ptr = &num;
printf("The value of ptr is: %d", ptr); // Compile error: expected expression before ',' token
  1. Incrementing or decrementing a pointer without considering the data type:
int arr[] = {1, 2, 3};
int *ptr = &arr[0];
ptr++; // Incorrect increment by size of an integer instead of an int array element
printf("The value of *ptr is: %d", *ptr); // Outputs: 4 (incorrect due to incorrect pointer arithmetic)
  1. Not checking for array bounds when using pointers:
int arr[] = {1, 2, 3};
int *ptr = &arr[0];
*ptr = 100; // Writing to an invalid memory location outside the array's bounds
printf("The value of arr is: %d\n", arr[3]); // Outputs garbage value (due to writing to uninitialized memory)
  1. Not initializing a pointer before using it:
int *ptr;
*ptr = 10; // Compile error: use of undeclared identifier 'ptr'

Practice Questions

  1. Write a function that swaps the first and last elements of an array.
  2. Write a program that finds the second largest number in an array.
  3. Write a program that reverses an array using pointers.
  4. Write a program that sorts an array using bubble sort algorithm with pointer implementation.

FAQ

  1. Why do we need pointers in C programming?
  • Pointers are essential for understanding low-level programming concepts and optimizing performance, as they allow us to manipulate memory directly. They are also necessary for working with dynamic memory allocation, functions, and complex data structures like linked lists and trees.
  1. What is the difference between a pointer and an array in C?
  • An array is a contiguous block of memory allocated for a specific data type, while a pointer is a variable that stores the memory address of another variable. Arrays are automatically initialized with their base address when declared, but pointers must be explicitly assigned memory addresses using the address-of operator (&).
  1. How can I check if a pointer is pointing to a valid memory location in C?
  • In C, there's no built-in function to check if a pointer points to a valid memory location. However, you can use conditional statements and bounds checking to ensure that your pointers are always pointing to valid locations. It's also important to avoid buffer overflows and other memory-related errors when working with pointers.
  1. What happens if I try to access an invalid memory location using a pointer in C?
  • Accessing an invalid memory location using a pointer can lead to undefined behavior, such as segmentation faults or memory corruption. It's essential to always check for array bounds and ensure that your pointers are pointing to valid locations to avoid these issues.