Back to C Programming
2026-07-145 min read

Access array elements

Learn Access array elements step by step with clear examples and exercises.

Why This Matters

In this extensive tutorial, we will delve deep into understanding and mastering the art of accessing array elements in C programming. Acquiring this skill is crucial for tackling real-world problems, excelling in exams or interviews, and demonstrating your proficiency in C programming.

Prerequisites

Before diving headfirst into the core concept, it's essential to have a strong foundation in:

  1. Basic C syntax (variables, operators, control structures, etc.)
  2. Data types in C (int, char, float, double, etc.)
  3. Memory management in C
  4. Pointers in C
  5. Arrays declaration and initialization in C
  6. Understanding of the difference between value type variables and reference type variables (structures, pointers)
  7. Understanding of pointer arithmetic
  8. Familiarity with functions and function parameters

Core Concept

An array is a collection of elements of the same data type stored at contiguous memory locations. Accessing an element within an array is achieved by using its index. In C, indices start from 0.

int arr[5] = {1, 2, 3, 4, 5}; // Declaring and initializing an array of integers
arr[0] = 1; // Accessing the first element (index 0)

Array Size and Index Bounds

The size of an array is specified during its declaration. Remember that the last index is one less than the array size, as indices start from 0.

int arr[5]; // Array with a size of 5
arr[4] = 5; // Accessing the last element (index 4)

Multidimensional Arrays

A multidimensional array is an extension of the single-dimensional array concept. To declare a multidimensional array, specify the number of dimensions and their respective sizes during declaration. For example:

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}}; // Declaring a 2D array with 2 rows and 3 columns

Dynamic Memory Allocation for Arrays

In cases where the size of an array is not known at compile time, dynamic memory allocation can be used to create arrays. This is achieved using the malloc() function.

int *arr = (int *) malloc(5 * sizeof(int)); // Dynamically allocating an array of 5 integers

Accessing Array Elements with Pointers

Accessing array elements can be done using pointers and pointer arithmetic. This approach is useful when dealing with dynamically allocated arrays or multidimensional arrays.

int arr[5] = {1, 2, 3, 4, 5}; // Declaring an array of integers
int *ptr = arr; // Creating a pointer to the first element
printf("%d\n", *ptr); // Accessing the first element using the pointer
ptr++; // Advancing the pointer to the next element
printf("%d\n", *ptr); // Accessing the second element using the pointer

Worked Example

Let's create a program that calculates the sum of all elements in an array using pointers and pointer arithmetic.

#include <stdio.h>

void calculateSum(int *arr, int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += *(arr + i); // Accessing each element using pointer arithmetic
}
printf("The sum of all elements is: %d\n", sum);
}

int main() {
int arr[5] = {1, 2, 3, 4, 5}; // Declaring and initializing an array of integers
calculateSum(arr, 5); // Passing the array to the function using a pointer
return 0;
}

Common Mistakes

  1. Accessing an invalid index: This can lead to a segmentation fault or undefined behavior.
int arr[5];
arr[6] = 6; // Invalid index, as the array size is 5
  1. Forgetting to initialize the array: If you don't initialize an array, all elements will be set to random values.
int arr[5];
printf("%d\n", arr[0]); // Outputs a random value, as the array was not initialized
  1. Using incorrect data types: Be sure to use the appropriate data type for each array element.
char arr[5] = {1, 2, 3, 4, 5}; // Error: char arrays can only hold characters (0-255 ASCII values)
  1. Not properly freeing dynamically allocated memory: If you allocate memory using malloc(), don't forget to deallocate it using free(). Failing to do so can lead to a memory leak.
int *arr = (int *) malloc(5 * sizeof(int)); // Dynamically allocating an array of 5 integers
// ...
free(arr); // Deallocating the dynamically allocated memory
  1. Not handling edge cases: Be aware of potential edge cases when working with arrays, such as empty or partially filled arrays.
int arr[5];
int size = 0; // Empty array
// ...
calculateSum(arr, size); // Passing an empty array to the function
  1. Misusing pointer arithmetic: Be careful when using pointer arithmetic, as it can lead to unexpected results if not used correctly.
int arr[5] = {1, 2, 3, 4, 5}; // Declaring an array of integers
int *ptr = arr; // Creating a pointer to the first element
ptr += 3; // Advancing the pointer by 3 elements (accessing the fourth element)
printf("%d\n", *ptr); // Outputs 4, as we accessed the fourth element instead of the third

Practice Questions

  1. Write a program that finds the maximum number in an array of integers using pointers and pointer arithmetic.
  2. Create a program that sorts an array of integers using bubble sort with pointer arithmetic.
  3. Write a program that checks if an array contains a specific value using pointers, loops, and pointer arithmetic.
  4. Write a program that finds the average of all elements in an array using pointers, dynamic memory allocation, and pointer arithmetic.
  5. Write a program that multiplies every element in an array by a constant factor using pointers, pointer arithmetic, and loops.
  6. Write a program that finds the second largest number in an array of integers using pointers and pointer arithmetic (handle edge cases).
  7. Write a program that reverses the order of elements in an array using pointers and pointer arithmetic.

FAQ

How do I declare a multidimensional array in C?

To declare a multidimensional array, specify the number of dimensions and their respective sizes during declaration. For example:

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}}; // Declaring a 2D array with 2 rows and 3 columns

How do I pass an array to a function in C?

To pass an array to a function, use the address-of operator (&) before the array name. For example:

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

int main() {
int arr[5] = {1, 2, 3, 4, 5}; // Declaring an array of integers
printArray(arr, 5); // Passing the array to the function
return 0;
}

How do I pass a multidimensional array to a function in C?

To pass a multidimensional array to a function, use pointers. For example:

void print2DArray(int arr[][3], int rows) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}

int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}}; // Declaring a 2D array with 2 rows and 3 columns
print2DArray(arr, 2); // Passing the 2D array to the function using pointers
return 0;
}