C Accessing Array Elements

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Accessing Array Elements?

Use array[index] to read or write an element. The expression a[i] is defined as *(a + i). C does not check bounds, so an index outside 0..n-1 is undefined behavior.

Remember
x = a[2];        /* read    */
a[0] = 99;       /* write   */
a[n - 1];        /* last    */
*(a + 2) == a[2] /* same thing */

Cheat Sheet

Keep these facts handy while you work with Accessing Array Elements in C.

TopicDetail
Readvalue = a[i];
Writea[i] = value;
Lasta[n - 1]
Equivalenta[i] is *(a + i)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Traversing Arrays.

Example 1 — Read, update, and pointer form

Two spellings access the same element.

C
#include <stdio.h>

int main(void) {
    int arr[] = {10, 20, 30, 40};
    int n = sizeof arr / sizeof arr[0];

    printf("first = %d, last = %d\n", arr[0], arr[n - 1]);
    arr[1] = 25;
    printf("arr[1] = %d\n", arr[1]);
    printf("*(arr + 2) = %d\n", *(arr + 2));
    return 0;
}

How It Works

  1. arr[n - 1] is the last element without hard-coding 3.
  2. Assigning arr[1] = 25 overwrites the old 20.
  3. *(arr + 2) moves two elements forward and dereferences.

Edge Cases & Pitfalls

Check these before relying on Accessing Array Elements in real programs.

oob

Index out of range

Reading arr[4] here is undefined. It may print garbage or crash. Validate indexes from user input.

neg

Negative indexes

arr[-1] reads memory before the array. It is legal syntax but almost always a bug.

Key Takeaways

  • Syntax: a[i] is *(a + i).
  • Range: only 0..n-1 is valid.
  • Safety: C will not stop you.

One line: Use array[index] to read or write an element.

Frequently Asked Questions

The behavior is undefined: you may read garbage, corrupt other data, or crash. Compilers and runtime tools such as AddressSanitizer can help detect it.
Technically yes, since both mean *(a + i), but write a[i] for readability.
Continue with "C Traversing Arrays" next. Visit every element with for loops, reverse loops, and pointer walks. The C Arrays hub lists every topic in order.

Next: Traversing Arrays

Visit every element with for loops, reverse loops, and pointer walks.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful