Arrays in C Programming

An array in C is a collection of elements of the same data type, stored in contiguous memory locations. Arrays provide a way to store and access multiple values using a single variable name with an index. Arrays are a fundamental part of C programming. They allow efficient storage and manipulation of multiple values using a single variable name. Whether you're dealing with a simple list or a complex grid of data, arrays provide a structured way to access and manage it. Mastery of arrays is essential for working with data structures, algorithms, file handling, and more in C.

Arrays are especially useful for storing large amounts of data like numbers, strings, or structures.

One-Dimensional Arrays

A one-dimensional array is the simplest form, essentially a list of elements, all of the same type.

Syntax:

data_type array_name[size];

To declare an array of 5 integers, we use int numbers[5];

Elements are accessed using an index, starting from 0:

numbers[0] = 10;

Two-Dimensional Arrays

A two-dimensional array is like a table or matrix, with rows and columns.

Syntax:

data_type array_name[rows][columns];

To declare a 3x3 matrix, we use int matrix[3][3];


Accessing Elements in Two-Dimensional Arrays

```c
matrix[0][0] = 1;
matrix[2][1] = 9;

Array Initialization

Arrays can be initialized at the time of declaration.

One-Dimensional Initialization:

int marks[5] = {90, 85, 88, 92, 76}; all the elements are initialized

int scores[5] = {1, 2}; If fewer values are provided, the remaining elements are set to zero:

Two-Dimensional Initialization:

int table[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

Partial Initialization:

Only some values can be initialized, others default to 0, for example int table[2][3] = {{1}, {4}};

Arrays and Functions

Arrays can be passed to functions as arguments to process their contents.

Syntax for Function Receiving One-Dimensional Array:

void function_name(data_type array_name[], int size);

Inside the function, the array behaves like a pointer.

Syntax for Function Receiving Two-Dimensional Array:

void function_name(data_type array_name[][columns], int rows);

For 2D arrays, the number of columns must be specified.

Arrays Returned from Functions:

  • Direct returning of arrays is not supported.
  • Instead, use global arrays or pass arrays as arguments to fill data.

Multidimensional Arrays

Multidimensional arrays have more than two dimensions, although 2D is most commonly used. A 3D array can be visualized as an array of matrices.

Syntax:

data_type array_name[size1][size2][size3];

int cube[2][3][4]; This declares a 3D array with:

  • 2 blocks (depth)
  • Each block has 3 rows
  • Each row has 4 columns

Initialization:

int cube[2][2][2] = {
    {
        {1, 2}, {3, 4}
    },
    {
        {5, 6}, {7, 8}
    }
};

Comparison of different Arrays

Array Type Dimensions Syntax Example Use Case
One-Dimensional 1D int arr[5]; Storing list of elements
Two-Dimensional 2D int matrix[3][4]; Tabular or matrix-like data
Multidimensional 3D+ int cube[2][3][4]; Complex data like 3D geometry

Key Points to Remember

  • Array indices start from 0.
  • Arrays in C have fixed size once declared.
  • Memory is allocated statically unless dynamic memory is used (via pointers).
  • Passing arrays to functions does not copy them; the function receives a reference (pointer) to the original array.