An array in C is a fixed-size homogeneous collection of elements stored at a contiguous memory location. It is a derived data type in C that can store elements of different data types such as int, char, struct, etc. It is one of the most popular data types widely used by programmers to solve different problems not only in C but also in other languages.
The properties of the arrays depend on the programming language. In this article, we will study the different properties of Array in the C programming language.
- Fixed Size Collection
- Homogeneous Elements
- Indexing in Array
- Dimensions of Array
- Contiguous Storage
- Random Access
- Array name relation with pointer
- Bound Checking
- Array Decay
C Array Properties
1. Fixed Size of an Array
In C, the size of an array is fixed after its declaration. It should be known at the compile time and it cannot be modified later in the program. The below example demonstrates the fixed-size property of the array.
Example:
C // C Program to Illustrate the Fixed Size Properties of the // Array #include <stdio.h> int main() { // creating a new array of size 5 int array[5] = { 1, 2, 3, 4, 5 }; printf("Size of Array Before: %d\n", sizeof(array) / sizeof(int)); // trying to increase the size of the array array[6]; // not checking the size printf("Size of Array After: %d", sizeof(array) / sizeof(int)); return 0; }
OutputSize of Array Before: 5 Size of Array After: 5
2. Homogeneous Collection
An array in C cannot have elements of different data types. All the elements are of the same type.
Example:
C // C program to Demonstrate the Homogeneous Property of the // C Array #include <stdio.h> int main() { // declaring integer array int arr[3] = { 1, 2 }; // trying to store string in the third element arr[2] = "Geeks"; // printing elements printf("Array[1]: %d\n", arr[0]); printf("Array[2]: %d\n", arr[1]); printf("Array[3]: %s", arr[2]); return 0; }
Output
main.c: In function ‘main’: main.c:12:16: warning: assignment to ‘int’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion] 12 | arr[2] = "Geeks"; | ^ main.c:17:28: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=] 17 | printf("Array[3]: %s", arr[2]); | ~^ ~~~~~~ | | | | char * int | %d Array[1]: 1 Array[2]: 2
3. Indexing in an Array
Indexing of elements in an Array in C starts with 0 instead of 1. It means that the index of the first element will be 0 and the last element will be (size - 1) where size is the size of the array.
Indexing of Elements in C Array Example:
C // C Program to Illustrate Array Indexing in C #include <stdio.h> int main() { // creating integer array with 2 elements int arr[2] = { 10, 20 }; // printing element at index 1 printf("Array[1]: %d\n", arr[1]); // printing element at index 0 printf("Array[0]: %d", arr[0]); return 0; }
OutputArray[1]: 20 Array[0]: 10
As we see in the above example, at index 1, the second element is present while at index 0, the first element is present.
4. Dimensions of the Array
An array in C can be a single dimensional like a 1-D array or multidimensional like a 2-D array, 3-D array, and so on. It can have any number of dimensions. The number of elements in a multidimensional array is the product of the size of all the dimensions.
Arrays of Different Dimensions Example:
C // C Program to create multidimensional array #include <stdio.h> int main() { // creating 2d array int arr2d[2][2] = { 1, 2, 3, 4 }; // creating 3d array int arr3d[2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8 }; printf("2D Array: "); // printing 2d array for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { printf("%d ", arr2d[i][j]); } } printf("\n3D Array: "); // printing 3d array for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { printf("%d ", arr3d[i][j][k]); } } } return 0; }
Output2D Array: 1 2 3 4 3D Array: 1 2 3 4 5 6 7 8
5. Contiguous Storage
All the elements in an array are stored at contiguous or consecutive memory locations. We can easily imagine this concept in the case of a 1-D array but multidimensional arrays are also stored contiguously. It is possible by storing them in row-major or column-major order where the row after row or column after the column is stored in the memory. We can verify this property by using pointers.
Example:
C++ // C Program to Verify the Contiguous Storage of Elements in // an Array #include <stdio.h> int main() { // creating an array of 5 elements int arr[5] = { 1, 2, 3, 4, 5 }; // defining pointers to 2 consecutive elements int* ptr1 = &arr[1]; int* ptr2 = &arr[2]; // printing the address of arr[1] and arr[2] printf("Address of arr[1] : %p\n", ptr1); printf("Address of arr[2] : %p", ptr2); return 0; }
OutputAddress of arr[1] : 0x7fffb8cc1ef4 Address of arr[2] : 0x7fffb8cc1ef8
In the above example, the difference between the addresses of arr[1] and arr[2] is 4 bytes which is the memory required to store a single integer. So, at memory addresses 0x7ffebc02e054 to 0x7ffebc02e057, arr[1] is stored and in the next 4 bytes, arr[2] is stored. The same is true for all the elements.
6. Random Access to the Elements
It is one of the defining properties of an Array in C. It means that we can randomly access any element in the array without touching any other element using its index. This property is the result of Contiguous Storage as a compiler deduces the address of the element at the given index by using the address of the first element and the index number.
Address of ith = Address of 1st Element + (Index * Size of Each Element)
Array in C We can verify this by using Pointer Arithmetic.
Example:
C // C Program to check the random access property of the // array #include <stdio.h> int main() { // creating an array of 5 elements int arr[5] = { 1, 2, 3, 4, 5 }; // address of first element int* ptr = &arr[0]; // printing arr[3] printf("Array[3]: %d\n", arr[3]); // printing element at index 3 using ptr printf("Array[3] using pointer to first element = %d", *(ptr + 3)); return 0; }
OutputArray[3]: 4 Array[3] using pointer to first element = 4
Note: We have not multiplied the size of each element in the code as the compiler deduce and multiply it automatically by itself whenever we perform pointer arithmetic.
7. Relationship between Array and Pointers
Arrays are closely related to pointers in the sense that we can do almost all the operations possible on an array using pointers. The array's name itself is the pointer to its first element.
Example:
C // C Program to Illustrate the Relationship Between Array // and Pointers #include <stdio.h> int main() { // creating an array with 3 elements int arr[3] = { 1, 2, 3 }; int* ptr = &arr[0]; // Pointer to first element printf("Pointer to First Element: %p\n", ptr); // Array name as pointer printf("Arran Name: %p", arr); return 0; }
OutputPointer to First Element: 0x7ffec5059660 Arran Name: 0x7ffec5059660
The relationship between pointer and array is very deep and we can study more about it in other articles such as - Pointer to an Array | Array Pointer
8. Bound Checking
Bound checking is the process in which it is checked whether the referenced element is present within the declared range of the Array. In C language, array bound checking is not performed so we can refer to the elements outside the declared range of the array leading to unexpected errors.
Example:
C // C Program to Illustrate the Out of Bound access in arrays #include <stdio.h> int main() { // creating new array with 3 elements int arr[3] = { 1, 2, 3 }; // trying to access out of bound element printf("Some Garbage Value: %d", arr[5]); return 0; }
OutputSome Garbage Value: 0
As seen in the above example, there is no error shown by the compiler while accessing memory that is out of array bounds.
9. Array Decay
Array decay is the process in which an array in C loses its dimension in certain conditions and decays into pointers. After this, we cannot determine the size of the array using sizeof() operator. It happens when an array is passed as a pointer.
Example:
C // C Program to Demonstrate the Array Decay #include <stdio.h> // function void func(int* arr) { printf("Sizeof Value in Function: %d", sizeof(arr)); } int main() { // creating array with 3 elements char arr[3]; printf("Sizeof Value in Main: %d\n", sizeof(arr)); // passing array func(arr); return 0; }
OutputSizeof Value in Main: 3 Sizeof Value in Function: 8
The size of the array in the main() is 3 bytes which is the actual size of the array but when we check the size of the array in func(), the size comes out to be 8 bytes which instead of being the size of the array, it is the size of the pointer to the first element of the array.
Similar Reads
C Arrays
An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., as well as derived and user-defined data types such as pointers, structures, etc. Creating an Array in
8 min read
Properties of Array in C
An array in C is a fixed-size homogeneous collection of elements stored at a contiguous memory location. It is a derived data type in C that can store elements of different data types such as int, char, struct, etc. It is one of the most popular data types widely used by programmers to solve differe
8 min read
Length of Array in C
The Length of an array in C refers to the maximum number of elements that an array can hold. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements. In C language, we don't have any pre-defi
3 min read
Multidimensional Arrays in C - 2D and 3D Arrays
A multi-dimensional array in C can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays include 2D arrays which grows in two dimensions, and 3D arrays which grows in three dimension
8 min read
Initialization of Multidimensional Array in C
In C, multidimensional arrays are the arrays that contain more than one dimensions. These arrays are useful when we need to store data in a table or matrix-like structure. In this article, we will learn the different methods to initialize a multidimensional array in C. The easiest method for initial
4 min read
Jagged Array or Array of Arrays in C with Examples
Prerequisite: Arrays in CJagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These type of arrays are also known as Jagged arrays. Example: arr[][] = { {0, 1, 2}, {6, 4}, {1, 7, 6, 8, 9}
3 min read
Pass Array to Functions in C
Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C. In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, wh
3 min read
How to pass a 2D array as a parameter in C?
A 2D array is essentially an array of arrays, where each element of the main array holds another array. In this article, we will see how to pass a 2D array to a function. The simplest and most common method to pass 2D array to a function is by specifying the parameter as 2D array with row size and c
3 min read
How to pass an array by value in C ?
In C programming, arrays are always passed as pointers to the function. There are no direct ways to pass the array by value. However, there is trick that allows you to simulate the passing of array by value by enclosing it inside a structure and then passing that structure by value. This will also p
2 min read
Variable Length Arrays (VLAs) in C
In C, variable length arrays (VLAs) are also known as runtime-sized or variable-sized arrays. The size of such arrays is defined at run-time. Variably modified types include variable-length arrays and pointers to variable-length arrays. Variably changed types must be declared at either block scope o
2 min read