Back to C Programming
2026-04-197 min read

14.11 Pointers and Arrays (C Programming)

Learn 14.11 Pointers and Arrays (C Programming) step by step with clear examples and exercises.

Title: Pointers and Arrays in C Programming - A full guide

Why This Matters

In this lesson, we delve into pointers and arrays, two fundamental concepts in C programming that are essential for understanding more complex data structures and algorithms. These topics are crucial for acing coding interviews, debugging real-world programs, and tackling challenging projects.

Prerequisites

Before diving into pointers and arrays, it is assumed you have a good grasp of the following concepts:

  1. Basic C syntax (variables, operators, control structures)
  2. Data types (int, float, char)
  3. Functions (declaration, definition, parameters, return values)
  4. Input/output operations (using scanf() and printf())
  5. Understanding of memory management in C

Core Concept

Pointers

A pointer is a variable that stores the memory address of another variable. In C, pointers are declared using the asterisk symbol *. The main use of pointers is to manipulate data stored in memory directly.

int num = 10; // Declare an integer variable named 'num' and initialize it with value 10
int *ptr; // Declare a pointer 'ptr' that can store the address of an integer variable
ptr = # // Assign the address of 'num' to 'ptr' using the address-of operator '&'
printf("The value of num is: %d\n", num); // Outputs: The value of num is: 10
printf("The address of num is: %p\n", &num); // Outputs: The address of num is: 0x7ffeefbff438 (on your system, this will be different)
printf("The value stored in the pointer ptr is: %p\n", ptr); // Outputs: The value stored in the pointer ptr is: 0x7ffeefbff438

Pointer Arithmetic

Pointer arithmetic allows you to perform operations like incrementing or decrementing a pointer. When you add or subtract an integer to/from a pointer, it moves by the size of the data type that the pointer points to.

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // Initialize ptr with the address of the first element in 'arr'
printf("The value stored at ptr is: %d\n", *(ptr)); // Outputs: The value stored at ptr is: 1
ptr++; // Move ptr to the next element (size of an int)
printf("The value stored at ptr after incrementing is: %d\n", *(ptr)); // Outputs: The value stored at ptr after incrementing is: 2

Arrays

An array is a collection of variables of the same data type, stored contiguously in memory. In C, arrays are declared using square brackets []. The first element of an array has index 0.

int arr[5] = {1, 2, 3, 4, 5}; // Declare an integer array 'arr' with 5 elements and initialize it with values 1-5
printf("The value of arr[0] is: %d\n", arr[0]); // Outputs: The value of arr[0] is: 1
printf("The size of the array arr is: %ld\n", sizeof(arr) / sizeof(arr[0])); // Outputs: The size of the array arr is: 5 (number of elements)

Dynamic Arrays

Dynamic arrays can be created using malloc() function to allocate memory dynamically. It's important to remember to free the allocated memory when it's no longer needed to avoid memory leaks.

int *dynamic_arr = (int *) malloc(n * sizeof(int)); // Allocate memory for n integers and store its address in dynamic_arr
// ... use 'dynamic_arr' as needed ...
free(dynamic_arr); // Free the allocated memory when it's no longer needed

Worked Example

Let's create a simple program that declares an array, initializes it with user input, and calculates the sum of its elements.

#include <stdio.h>

int main() {
int n, i, sum = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers for the array:\n", n);
for (i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
sum += arr[i]; // Add each element to the running total
}
printf("The sum of the elements in the array is: %d\n", sum);
return 0;
}

Common Mistakes

  1. Forgetting to initialize an array or a pointer before using it.
  2. Using an uninitialized pointer or accessing an out-of-bounds array element.
  3. Incorrectly declaring the size of a dynamic array (e.g., int arr[]; instead of int arr[size];).
  4. Neglecting to include proper header files, such as ``.
  5. Mixing up pointer arithmetic and array indexing.
  6. Failing to free dynamically allocated memory when it's no longer needed (memory leaks).
  7. Not checking for null pointers before dereferencing them.

Common Mistakes - Dynamic Memory Allocation

  1. Forgetting to check if the allocation was successful (e.g., malloc() returned NULL) and handling the error appropriately.
  2. Failing to allocate enough memory for a large array, leading to undefined behavior or segmentation faults.
  3. Not properly initializing dynamically allocated arrays before using them.
  4. Leaking memory by not freeing dynamically allocated memory when it's no longer needed.

Practice Questions

  1. Write a program that sorts an array of integers in ascending order using bubble sort.
  2. Implement a function that reverses an array of characters.
  3. Create a program that finds the second largest number in an array.
  4. Write a function that takes two arrays as arguments and returns their sum element-wise.
  5. Write a program to find the maximum and minimum elements in a dynamically allocated array.
  6. Implement a dynamic memory allocation function for a stack data structure.
  7. Create a linked list implementation using pointers, with functions to insert, delete, and traverse nodes.
  8. Write a program that calculates the factorial of a number using recursion and pointers.
  9. Implement a binary search algorithm using pointers in an array.
  10. Write a function that takes two strings as arguments and concatenates them using pointers.

FAQ

Q: What happens when we declare a pointer without initializing it?

A: When you declare a pointer without initializing it, the value stored in the pointer is undefined and accessing it will lead to undefined behavior.

Q: How can I find the maximum element in an array using pointers?

A: You can use two pointers, one to traverse the array and find the maximum element, and another to keep track of the current maximum found so far.

  1. Q: What is the difference between a pointer and a reference? (Bonus points for explaining this concept in C++)

A: In C++, references are similar to pointers but have some key differences. While a pointer stores the memory address of another variable, a reference is an alias for another variable. References are more convenient to use since they behave like regular variables and cannot be null or uninitialized.

Q: How can I find the minimum element in an array using pointers?

A: Similar to finding the maximum element, you can use two pointers to traverse the array and keep track of the current minimum found so far.

Q: What is pointer arithmetic in C?

A: Pointer arithmetic allows you to perform operations like incrementing or decrementing a pointer. When you add or subtract an integer to/from a pointer, it moves by the size of the data type that the pointer points to.

Q: How can I allocate memory for a dynamically-sized array in C?

A: You can use the malloc() function to dynamically allocate memory for an array and store its address in a pointer variable. Don't forget to free the allocated memory when it's no longer needed to avoid memory leaks.

Q: What is the difference between call by value and call by reference in C++?

A: In call by value, the function receives a copy of the argument, while in call by reference, the function receives an alias (reference) to the original variable. This allows changes made within the function to affect the original variable.

Q: How can I implement a stack data structure using pointers in C?

A: You can create a struct for a node that contains a data element and a pointer to the next node. Implement functions to push, pop, and traverse the stack using these nodes and pointers.

Q: What is a null pointer in C?

A: A null pointer (NULL or 0) has no memory address associated with it and is used to indicate that a pointer does not point to any valid memory location.

Q: How can I implement a binary search algorithm using pointers in an array?

A: You can use two pointers, one pointing to the first element and another pointing to the last element of the array. Compare the middle element with the target value and update the pointers accordingly until you find the target or the pointers cross each other.