Nearby lessons

55 of 124

C - Access Array Elements

How to access and modify array elements in C using the subscript operator [], why arr[i] is really *(arr + i), and how to stay inside the array bounds.

Reading Elements

Put the index in square brackets after the array name. Any integer expression works as the index:

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int nums[5] = {10, 20, 30, 40, 50};
6 
7 printf("First : %d\n", nums[0]);
8 printf("Third : %d\n", nums[2]);
9 printf("Last : %d\n", nums[4]);
10 printf("Sum of first two: %d\n", nums[0] + nums[1]);
11 
12 int i = 3;
13 printf("Using a variable: nums[%d] = %d\n", i, nums[i]);
14 printf("Using an expression: nums[1+2] = %d\n", nums[1 + 2]);
15 return 0;
16}
Output
First  : 10
Third  : 30
Last   : 50
Sum of first two: 30
Using a variable: nums[3] = 40
Using an expression: nums[1+2] = 40

Writing to Elements

The same arr[i] expression on the left of = stores a value. An array element behaves exactly like an ordinary variable:

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int nums[5] = {10, 20, 30, 40, 50};
6 int i;
7 
8 nums[0] = 100; /* replace */
9 nums[2] += 5; /* modify in place */
10 nums[4]++; /* increment */
11 
12 for (i = 0; i < 5; i++) printf("%d ", nums[i]);
13 printf("\n");
14 return 0;
15}
Output
100 20 35 40 51 

Accessing Every Element with a Loop

The for loop is the natural partner of an array. Note the condition i < size, never i <= size:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int nums[6] = {12, 45, 7, 89, 23, 56};
6 int size = sizeof(nums) / sizeof(nums[0]);
7 int i, max = nums[0];
8 
9 /* forwards */
10 printf("Forward : ");
11 for (i = 0; i < size; i++) printf("%d ", nums[i]);
12 
13 /* backwards */
14 printf("\nReverse : ");
15 for (i = size - 1; i >= 0; i--) printf("%d ", nums[i]);
16 
17 /* find the largest */
18 for (i = 1; i < size; i++)
19 if (nums[i] > max) max = nums[i];
20 
21 printf("\nLargest : %d\n", max);
22 return 0;
23}
Output
Forward : 12 45 7 89 23 56
Reverse : 56 23 89 7 45 12
Largest : 89

What [] Really Does

The array name is the address of the first element. The subscript operator is defined as pointer arithmetic:

arr[i] means *(arr + i) — "go i elements past the start, then read what is there".

Example04
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int nums[5] = {10, 20, 30, 40, 50};
6 
7 printf("nums[2] = %d\n", nums[2]);
8 printf("*(nums + 2) = %d\n", *(nums + 2)); /* identical */
9 printf("2[nums] = %d\n", 2[nums]); /* legal but awful */
10 
11 printf("&nums[0] = %p\n", (void *) &nums[0]);
12 printf("nums = %p\n", (void *) nums); /* the same address */
13 return 0;
14}
Output
nums[2]      = 30
*(nums + 2)  = 30
2[nums]      = 30
&nums[0] = 000000000061FE00
nums     = 000000000061FE00

Why 2[nums] Compiles

In simple words: because arr[i] is defined as *(arr + i), and addition is commutative, *(2 + nums) is the same as *(nums + 2). So 2[nums] works. It is a famous C curiosity — never write it in real code, but it proves that [] is pure pointer arithmetic.

Validating an Index

When the index comes from the user, check it yourself. C will not:

Example06
CCode Cell
1#include <stdio.h>
2 
3#define SIZE 5
4 
5int main()
6{
7 int nums[SIZE] = {10, 20, 30, 40, 50};
8 int index;
9 
10 printf("Enter an index (0 to %d): ", SIZE - 1);
11 scanf("%d", &index);
12 
13 if (index >= 0 && index < SIZE)
14 printf("nums[%d] = %d\n", index, nums[index]);
15 else
16 printf("Error: index %d is out of range.\n", index);
17 return 0;
18}
Output
Enter an index (0 to 4): 7
Error: index 7 is out of range.

Common Mistakes

  • i <= size in the loop condition — reads one element past the end every single time.
  • Starting at i = 1 — silently skips the first element.
  • Using & when readingprintf("%d", &nums[0]) prints an address, not the value.
  • Forgetting & when scanningscanf("%d", nums[i]) crashes; it needs &nums[i].
  • Negative indexesarr[-1] compiles and reads memory before the array.
The classic off-by-one: for (i = 0; i <= 5; i++) on int a[5] touches a[5], which does not exist. Use i < 5. This single character is responsible for an enormous share of real-world C bugs.
📝 Key Takeaways
  • arr[i] both reads and writes element i.
  • arr[i] is exactly equivalent to *(arr + i).
  • Valid indexes run from 0 to size - 1.
  • Because arr[i] == *(arr+i), the odd form i[arr] also compiles.
  • Always validate an index that comes from user input.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4