Back to C Programming
2026-07-136 min read

Example 1: Pointers and Arrays (C Programming)

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

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

Why This Matters

In this lesson, we delve into the intricate relationship between arrays and pointers in C programming. Understanding this concept is crucial for writing efficient programs, troubleshooting real-world bugs, and acing coding interviews. By the end of this tutorial, you'll be well-equipped to manipulate memory effectively and create dynamic data structures.

Prerequisites

Before diving into pointers and arrays, it is essential to have a solid grasp of the following C programming concepts:

  1. Variables and Data Types
  2. Control Structures (if...else statements, for loops)
  3. Functions
  4. Basic Input/Output (printf(), scanf())
  5. Understanding memory allocation in C
  6. Knowledge of structures and unions (optional but recommended)

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 programming, arrays are declared using square brackets []. For example:

int arr[5]; // Declares an array named 'arr' with 5 integer elements

Pointers in C Programming

A pointer is a variable that stores the memory address of another variable. In C programming, pointers are declared using the asterisk *. For example:

int *ptr; // Declares a pointer named 'ptr' that can store the address of an integer

The Relationship Between Arrays and Pointers

Every array in C has a corresponding pointer, which points to the first element of the array. This is known as the base address or starting address of the array. For example:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;

In this example, arr is an array with 5 integer elements, and ptr is a pointer that stores the base address of arr. The value stored in ptr can be used to access any element of the array using pointer arithmetic.

Array Indices vs Pointer Arithmetic

When accessing array elements using indices, you are essentially calculating an offset from the base address of the array. For example:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d\n", *(ptr + 1)); // Accesses the second element of the array using pointer arithmetic

Pointer Dereferencing

Dereferencing a pointer means accessing the value stored at the memory address it points to. For example:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d\n", *ptr); // Accesses and prints the first element of the array using pointer dereferencing

Pointer Arithmetic

Pointer arithmetic allows you to perform operations on pointers that move them to different memory locations. For example:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
ptr++; // Moves the pointer to the next element (4 bytes on most systems)
printf("%d\n", *ptr); // Accesses and prints the second element of the array using pointer arithmetic

Pointer Types and Address-of Operator (&)

In C, there are different types of pointers for various data types. For example:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr_int; // Pointer to an integer
float *ptr_float; // Pointer to a float
char *ptr_char; // Pointer to a character

To obtain the memory address of a variable, you can use the address-of operator (&). For example:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr_int = &arr[0]; // ptr_int stores the base address of arr
printf("%p\n", ptr_int); // Prints the memory address of the first element of the array

Worked Example

Let's create a simple program that demonstrates how to manipulate arrays using pointers:

#include <stdio.h>

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

int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;

printArray(arr, 5); // Prints the array elements using a function

printf("First element: %d\n", *ptr); // Accesses and prints the first element of the array using pointer dereferencing

*(ptr + 1) = 10; // Changes the second element of the array using pointer arithmetic

printArray(arr, 5); // Prints the modified array elements

return 0;
}

In this example, we define a function printArray() to print an array. We then create an array arr, declare a pointer ptr that points to the first element of arr, and use ptr to access and modify elements of the array. The output of this program is:

1 2 3 4 5
First element: 1
1 10 3 4 5

Common Mistakes

  1. Forgetting the base address: When using pointers to access array elements, it's essential to remember that the pointer points to the base address of the array. For example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr + 1; // Incorrect! ptr points to the second element + 1 byte (4 bytes on most systems), not the third element
printf("%d\n", *ptr); // Outputs garbage value or a segmentation fault
  1. Incorrect pointer arithmetic: When performing pointer arithmetic, it's essential to remember that each element in an array occupies one unit of its data type size. For example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
ptr++; // Correct! Moves the pointer to the next element (4 bytes on most systems)
*(ptr - 1) = 10; // Incorrect! Accesses the previous element, which is not defined in this case
  1. Ignoring array bounds: It's crucial to ensure that pointers do not access elements outside the boundaries of an array. For example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
*(ptr + 5) = 10; // Accesses an element outside the array boundaries (index 4), resulting in undefined behavior

Practice Questions

  1. Write a function that takes an integer array and its size as arguments, sorts the array using bubble sort, and returns the sorted array.
  2. Implement a function that reverses an array of integers in-place without using any additional memory.
  3. Create a program that finds the second largest number in an array of integers.
  4. Write a function that takes an integer pointer and an integer as arguments, and increments all elements in the array pointed to by the pointer by the given value.
  5. Implement a function that copies an array of characters into another array, with the possibility of padding the destination array with zeros if it is not large enough to hold the source array.
  6. Write a program that reads a line of text from the user and counts the frequency of each unique character in the input.
  7. Implement a function that finds the maximum sum of contiguous subarray within an integer array.
  8. Create a program that generates all permutations of a given string.
  9. Write a function that checks if two strings are anagrams of each other (i.e., they have the same characters but possibly in different orders).
  10. Implement a function that finds the longest common subsequence between two strings.

FAQ

  1. Why use pointers with arrays in C?

Pointers allow for dynamic memory allocation, efficient access to array elements, and the creation of flexible data structures like linked lists and trees.

  1. How can I determine the size of an array at runtime in C?

In C, arrays do not have a built-in way to determine their size at runtime. However, you can pass the size as a separate argument or use preprocessor directives like sizeof() to calculate the size of an array or its elements.

  1. What are some common pitfalls when using pointers and arrays in C?

Common pitfalls include forgetting the base address, incorrect pointer arithmetic, and ignoring array bounds. It's essential to understand these issues to write robust and efficient C programs.

  1. How can I initialize an array with a specific value using a loop in C?

You can use a for loop to initialize an array with a specific value. For example:

int arr[5];
for (int i = 0; i < 5; ++i) {
arr[i] = 5;
}

This will set all elements of arr to the value 5.