Back to C Programming
2026-03-305 min read

Subscript operator

Learn Subscript operator step by step with clear examples and exercises.

Why This Matters

Understanding the subscript operator in C programming is essential for navigating arrays efficiently and effectively. It allows you to access individual elements within an array, making it possible to perform various operations such as reading, writing, and manipulating data stored in arrays. Mastering this concept will help you tackle real-world problems with ease, whether you're working on a competitive programming platform, preparing for an interview, or just writing code for fun.

Prerequisites

Before diving into the subscript operator, it is essential to have a solid understanding of the following concepts:

  1. Variables and Data Types: Basic knowledge of data types like int, char, float, and pointers is necessary to work with arrays.
  2. Pointers: Familiarity with pointer variables and pointer arithmetic will help you grasp how the subscript operator works under the hood.
  3. Arrays: Understanding the concept of arrays, their declaration, and initialization is vital for using the subscript operator effectively.

Core Concept

The subscript operator ([]) in C allows us to access elements within an array. The syntax for accessing an element arr[i] involves two parts: a pointer expression that points to the beginning of the array, and an integer expression that specifies the index of the desired element.

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

Array Subscript Expression

The subscript operator expression has two forms:

  1. pointer-expression [ integer-expression ]
  2. integer-expression [ pointer-expression ]

In both cases, pointer-expression is an expression of type pointer to complete object, and integer-expression is an expression of integer type. When using the subscript operator on an array, the pointer expression undergoes lvalue-to-rvalue conversion to become a pointer to the first element of the array.

Lvalue-to-Rvalue Conversion

When an lvalue (a modifiable value) is used in an expression where a rvalue (a temporary value) is expected, it undergoes lvalue-to-rvalue conversion. In the case of an array, the entire array expression becomes a pointer to its first element after this conversion.

Example

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr points to the first element of arr
ptr + 2; // The result is a pointer to the third element (index 2)
arr[2]; // Equivalent to *(ptr + 2), which gives us the value 3

Worked Example

Let's create a simple program that initializes an array of integers, prints its elements using the subscript operator, and then modifies some values.

#include <stdio.h>

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

printf("Original array:\n");
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}

// Modifying some values using the subscript operator
arr[2] = 100;
arr[4] = -1;

printf("\nModified array:\n");
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}

return 0;
}

Output:

Original array:
1 2 3 4 5
Modified array:
1 2 100 -1 5

Common Mistakes

  1. Index Out of Range: Always ensure that the index you use is within the bounds of the array to avoid segmentation faults and undefined behavior.
int arr[5] = {1, 2, 3, 4, 5};
arr[6]; // This will cause a segmentation fault!
  1. Mixed Data Types: Be careful when mixing data types within an array and using the subscript operator, as it may lead to unexpected results.
int arr[3] = {1, 2.5, 'a'}; // This is invalid!
arr[1]; // The value of arr[1] will be garbage due to type mismatch.

Common Mistakes (continued)

  1. Array Overflow and Underflow: Be aware of array boundaries when using the subscript operator to avoid overflow (index greater than the last index) or underflow (index less than zero).
int arr[5] = {1, 2, 3, 4, 5};
arr[-1]; // This will cause an error as the array starts from index 0.
arr[6]; // This will also cause a segmentation fault as the last valid index is 4.

Practice Questions

  1. Write a program that initializes an array of integers and finds the sum of its elements using the subscript operator.
  2. Given an array of strings, write a function that returns the length of the longest string in the array using the subscript operator.
  3. Write a program that sorts an array of integers using bubble sort and prints the sorted array using the subscript operator.
  4. Write a program that finds the second-highest number in an unsorted array using the subscript operator.
  5. Implement a function that takes an array of integers as input, checks if it is sorted in ascending order using the subscript operator, and returns 1 if it is sorted or 0 otherwise.

FAQ

  1. What happens if I use the subscript operator on a pointer that doesn't point to an array?

Using the subscript operator on a pointer that doesn't point to an array will result in undefined behavior, as the memory location it points to may not be valid for an array element.

  1. Can I use the subscript operator with multidimensional arrays?

Yes! The subscript operator can be used with multidimensional arrays by specifying multiple index expressions within square brackets, separated by commas.

  1. Is it possible to use the subscript operator on a constant pointer?

No, you cannot use the subscript operator on a constant pointer because the pointer value would not be modifiable, and changing the array element through the subscript operator would require a modifiable pointer.

  1. Can I use the subscript operator to assign values to an entire array at once?

Yes! You can use the subscript operator to initialize an entire array or modify all its elements by using an initializer list with braces {}. For example:

int arr[5] = {1, 2, 3, 4, 5}; // Initializing an array with the subscript operator
  1. Can I use the subscript operator to check if an array is empty?

Yes! You can use the subscript operator to check if an array is empty by attempting to access its first element and checking for a null pointer. However, it's more common to initialize arrays with a specific size and check if the size is zero instead:

int arr[0]; // An empty array
arr[0]; // This will cause a segmentation fault as the array is empty

if (sizeof(arr) / sizeof(arr[0]) == 0) {
printf("Array is empty.\n");
}