Skip to main content

C-language

C Arrays, behind the scene !!

An Array in C is a collection of similar types of data elements. It is a built-in data type and is widely used by developers. Here, we will cover the array declarations, the memory layout, array decay, and multi-dimensional arrays.

Array Declaration

The array is declared with the following syntax -

dataType arrayName [array_size];

An array can be defined by specifying the initial value of the array elements, the compiler can identify the array size if not specified explicitly.

Array definitions and declarations.

šŸ’”
C99 and beyond support dynamic array declaration. The size of the array depends on the user-provided input.

Memory Layout

A contiguous memory space is allocated for array elements and can be accessed via an array index.

Displaying IMG_0849.jpeg
Memory layout
šŸ’”
The array size is a compile-time constant!

The ith element of an array can be accessed in a constant time via arr[i]. It is located at the address arr + i * sizeof(int). Here, the arr is the address of the first element in the array, in the above case 0x400.

arr, &arr, and &arr[0] all yield the same value (the address of the first element of the array and it is the same as the address of the array itself). Hence, arr[i] is effectively translated to *(arr + i) i.e. dereferencing element at the address (a+i).

According to the pointer arithmetic arr + i will be calculated to arr + i * sizeof(int).

arrays.c
šŸ¤Æ
Note: arr[i] = *(arr + i) = *(i + arr) = i[arr]

One can calculate the array size using sizeof() function. Where does array size come from? Is it stored anywhere?

Answer - No!

The arr is an object of type int[5], which is well known to the compiler at compile time. When you ask the compiler sizeof(arr) it returns 5 * sizeof(int) = 5 * 4 = 20. So the array size can be calculated with sizeof(arr) / sizeof(int).

Array and Pointers

Array and pointers are generally used interchangeably. An array passed to a function is normally captured as a pointer to the data type. When an array is passed, the address of the first element is passed and captured in a pointer.

Array Decay