![]() |
VOOZH | about |
An array is a linear data structure that stores a fixed-size sequence of elements of the same data type in contiguous memory locations. Each element can be accessed directly using its index, which allows for efficient retrieval and modification.
2 4 8 12 16 18
The below image shows the array created in the above program.
To understand the key characteristics of arrays such as fixed size, contiguous memory allocation, and random access. Refer to this article: Properties of Arrays
The whole process of creating an array can be divided into two primary sub processes i.e.
Array declaration is the process of specifying the type, name, and size of the array. In C, we have to declare the array like any other variable before using it.
When we declare an array in C, the compiler allocates the memory block of the specified size to the array name.
When the array is declared or allocated memory, the elements of the array contain some garbage value. So, we need to initialize the array to some meaningful values.
Array in C provides random access to its elements, which means that we can access any element of the array by providing the position of the element, called the index.
8 16 2
We can update the value of array elements at the given index i in a similar way to accessing an element by using the array square brackets [] and assignment operator (=).
1
Array Traversal is the process in which we visit every element of the array in a specific order. For C array traversal, we use loops to iterate through each element of the array.
Printing Array Elements 2 4 8 12 16 Printing Array Elements in Reverse 16 12 8 4 2
The size of the array refers to the number of elements that can be stored in the array. The array does not contain the information about its size but we can extract the size using sizeof() operator.
5
The sizeof() operator returns the size in bytes. sizeof(arr) returns the total number of bytes of the array. In an array, each element is of type int, which is 4 bytes. Therefore, we can calculate the size of the array by dividing the total number of bytes by the byte size of one element.
Note: This method only works in the scope in which the array is declared. Refer to this article to know more - Length of Array in C
To know how to pass an array to a function in C, refer to this article — Arrays and Pointers
For working with tables, matrices, or grids, C allows the use of arrays with more than one dimension. To learn more, refer to this article — Multidimensional Arrays in C.
The following problems helps you to improve your efficiency in using C array: