What are Arrays in C?

// Quick Answer
  • An array is a collection of elements of the same type.
  • All elements are stored in contiguous memory locations.
  • They are accessed using index numbers starting from 0.

What is an array?

In C, an array is a data structure that stores multiple values of the same data type in a single variable.

Instead of creating many variables, you use one array to store all values together.

๐Ÿ’ก Simple idea

An array is like a row of boxes, each holding one value.

Why use arrays?

  • ๐Ÿ“ฆ Store multiple values in one variable
  • โšก Faster access using indexes
  • ๐Ÿงน Cleaner and organized code
  • ๐Ÿ“Š Useful for loops and data processing

Declaring an array

int numbers[5];

This creates an array that can store 5 integers.

Initializing an array

int numbers[5] = {10, 20, 30, 40, 50};

Accessing array elements

Array elements are accessed using index numbers (starting from 0).

printf("%d", numbers[0]);  // 10
printf("%d", numbers[2]);  // 30

How arrays work in memory

  • All elements are stored in consecutive memory locations
  • Each element has the same data type
  • Index helps calculate memory address

Looping through arrays

for(int i = 0; i < 5; i++) {
  printf("%d\n", numbers[i]);
}

Types of arrays

  • 1D arrays โ€” single row
  • 2D arrays โ€” rows and columns (matrices)
  • Multi-dimensional arrays

Example of 2D array

int matrix[2][2] = {
  {1, 2},
  {3, 4}
};

Common mistakes

  • Accessing out-of-bounds index
  • Not initializing arrays properly
  • Confusing size and index
โš ๏ธ Important

Array indexes in C always start from 0. Accessing invalid indexes can cause undefined behavior.

Summary

Arrays in C are used to store multiple values of the same type in a single variable, using continuous memory and index-based access.

They are one of the most important and widely used data structures in C programming.