Back to C Programming
2026-07-135 min read

Array and Pointer Relation (C Programming)

Learn Array and Pointer Relation (C Programming) step by step with clear examples and exercises.

Why This Matters

Understanding the relationship between arrays and pointers in C programming is crucial for several reasons:

  1. Efficient Data Management: Arrays provide a convenient way to store multiple values of the same data type, while pointers enable direct manipulation of memory locations, leading to more efficient programs when dealing with large datasets or dynamic memory allocation.
  1. Job Interviews and Competitive Coding: Mastering arrays and pointers is essential for demonstrating proficiency in C programming during job interviews and competitive coding challenges.

Prerequisites

Before diving into arrays and pointers, you should have a solid understanding of the following C programming basics:

  • Variables and data types
  • Operators
  • Control structures (if-else, loops)
  • Functions
  • Basic memory management concepts (stack and heap)

Core Concept

Arrays in C Programming

An array is a collection of elements of the same data type stored at contiguous memory locations. In C, arrays are defined using square brackets []. The first element's index is 0, and the size of an array can be determined using the sizeof operator.

int arr[5]; // Declares an array 'arr' with 5 elements of type int
printf("%d\n", sizeof(arr)); // Output: 20 (assuming int is 4 bytes and a system pointer size of 8 bytes)

Pointers in C Programming

A pointer is a variable that stores the memory address of another variable. In C, pointers are declared using the * symbol. They provide flexibility by allowing direct manipulation of memory locations.

int num = 10;
int *ptr = # // Declares a pointer 'ptr' pointing to the memory location of 'num'
printf("%d\n", *ptr); // Output: 10 (since ptr points to the value stored in 'num')

Array and Pointer Relationship

An array name is actually a constant pointer to the first element of the array. This means that you can use an array name wherever a pointer is expected, and vice versa.

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // Assigns the address of 'arr[0]' to 'ptr'
printf("%d\n", *ptr); // Output: 1 (since ptr points to the first element of 'arr')

By using pointers with arrays, you can manipulate array elements dynamically and perform operations like swapping elements or finding an element in an array more efficiently.

Dynamic Memory Allocation

C provides functions like malloc(), calloc(), and realloc() to dynamically allocate memory for arrays (or other data structures) using pointers. It's essential to remember to free the allocated memory when it is no longer needed using the free() function.

int *arr = (int *)malloc(10 * sizeof(int)); // Allocates memory for 10 integers and assigns a pointer to 'arr'
// ... use arr ...
free(arr); // Free the allocated memory

Worked Example

Let's write a program that sorts an array of integers using the bubble sort algorithm. We will use pointers to simplify the process.

#include <stdio.h>

void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (*(arr + j) > *(arr + j + 1)) {
swap((arr + j), (arr + j + 1));
}
}
}
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, size);
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]);
}
return 0;
}

In this example, we define a swap() function that takes two pointers and swaps the values they point to. The bubbleSort() function sorts an array using the bubble sort algorithm with pointer arithmetic. In the main() function, we initialize an array, calculate its size, sort it, and print the sorted array.

Common Mistakes

  1. Forgetting to include necessary header files (e.g., stdio.h)
  2. Not initializing arrays before using them
  3. Using invalid or out-of-bounds indices when accessing array elements
  4. Declaring pointers without assigning memory (i.e., forgetting to use malloc() or similar functions)
  5. Not dereferencing a pointer properly when assigning or accessing its value (e.g., using *ptr = num; instead of *ptr = &num;)
  6. Incorrectly comparing pointers for equality or inequality (pointers are not equal unless they point to the same memory location)
  7. Failing to free dynamically allocated memory when it is no longer needed, leading to memory leaks
  8. Using pointers without understanding their underlying memory addresses and potential issues related to pointer arithmetic

Practice Questions

  1. Write a program that finds the maximum number in an array using pointers.
  2. Implement a function that reverses an array using pointers.
  3. Write a program that checks if an array contains a specific value using pointers.
  4. Implement a function that finds the second-largest number in an array using pointers and without using additional memory.
  5. Given two arrays, write a program that finds their intersection (i.e., common elements) using pointers.
  6. Write a program that sorts an array of structures using pointers and a custom comparison function.
  7. Implement a function that merges two sorted arrays using pointers and pointer arithmetic.
  8. Write a program that counts the occurrences of each unique element in an array using pointers and hashtables (hash maps).
  9. Given a matrix, write a program that finds its transpose using pointers and pointer arithmetic.
  10. Implement a function that finds the kth smallest element in an unsorted array using pointers and quickselect algorithm.

FAQ

What happens if I try to dereference a null pointer?

Dereferencing a null pointer results in undefined behavior and can cause your program to crash or behave unexpectedly. To avoid this, always ensure that a pointer points to valid memory before dereferencing it.

Can I use pointers with multi-dimensional arrays?

Yes, you can use pointers with multi-dimensional arrays by treating them as one-dimensional arrays of pointers or using pointer arithmetic to access elements.

Is it safe to compare two pointers for equality in C?

In general, comparing two pointers for equality is unsafe because their values depend on the memory layout and can change during runtime. However, if both pointers point to the same array or contiguous memory locations, they will be equal.

Why do we use pointers with arrays instead of just using indices?

Pointers provide more flexibility when working with arrays because they allow direct manipulation of memory locations. Using pointers can make certain operations like swapping elements or finding an element in an array more efficient and easier to implement.

How do I free dynamically allocated memory in C using pointers?

To free dynamically allocated memory, use the free() function with the pointer that was used to allocate the memory. For example:

int *arr = (int *)malloc(10 * sizeof(int)); // Allocates memory for 10 integers and assigns a pointer to 'arr'
// ... use arr ...
free(arr); // Free the allocated memory