Features Of Array In C
In C, an array is a regular, predetermined group of elements stored in a single memory block. In the C programming language, it is a derived data type that may store pieces of several different data types, including int, char, struct, and others.
1. Indexing in an Array:
In C, indexing starts at 0 instead of 1 for array entries. It indicates that, if size is the array's size, the first element will have an index of 0 and the last component will have an index of (size - 1).
Example:
#include <stdio.h>
int main()
{
int arr[4] = {1, 2, 3, 4 };
printf("Array at index arr[1]: %d\n", arr[1]);
printf("Array at index arr[0]: %d", arr[0]);
return 0;
}
OUTPUT:
![Features Of Array In C](https://static.tutorialandexample.com/c/features-of-array-in-c1.png)
2.Bound Checking:
The process of "bound checking" is ascertaining whether the referenced element is indeed present inside the declared range of the Array.
Because the C programming language does not provide array bound checking, we are able to refer to elements above the defined range of the array, which may lead to errors or other unexpected failures.
Example:
#include <stdio.h>
int main()
{
int arr[4] = { 2, 5, 8, 9 };
printf("Garbage Value is displayed resulting array index out-of-bound: %d", arr[10]);
return 0;
}
OUTPUT:
![Features Of Array In C](https://static.tutorialandexample.com/c/features-of-array-in-c2.png)
3. Homogeneous items:
A C array is not allowed to contain elements of more than one data type. Each component is a member of the same sort.
Example:
#include <stdio.h>
int main()
{
int arr[5] = { 1, 2, 3, 4 };
arr[4] = "abc";
printf("Array[1]: %d\n", arr[0]);
printf("Array[2]: %d\n", arr[1]);
printf("Array[3]: %d\n", arr[2]);
printf("Array[4]: %d\n", arr[3]);
printf("Array[5]: %s", arr[4]);
return 0;
}
OUTPUT:
![Features Of Array In C](https://static.tutorialandexample.com/c/features-of-array-in-c3.png)
4.Random Access:
In C, one of the features that sets Arrays apart is random access. According to this, we can access any element in the array at random using an element's index without affecting other items in the array.
By using the address of the first element and the index number to locate the address of the element at the provided index, a compiler can locate this property, which was generated by Contiguous Storage.
Example:
#include <stdio.h>
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int* ptr = &arr[0];
printf("Array at index[3]: %d\n", arr[2]);
printf("Array at index[3] using pointer to first element = %d",*(ptr + 2));
return 0;
}
OUTPUT:
![Features Of Array In C](https://static.tutorialandexample.com/c/features-of-array-in-c4.png)
5. Contiguous Storage:
The elements of an array are stored in memory sections that are sequential or continuous.
This concept is easy to understand because of one-dimensional arrays, but multidimensional arrays are capable of being stored in a contiguous fashion.
It is possible by putting them in row-major order or column-major order, respectively, so that they are kept arranged in memory. We can validate this property through the usage of pointers.
Example:
#include <stdio.h>
int main()
{
int arr[4] = { 2, 4, 6, 8 };
int* ptr_1 = &arr[1];
int* ptr_2 = &arr[2];
printf("Address of array[1] : %p\n", ptr_1);
printf("Address of array[2] : %p", ptr_2);
return 0;
}
OUTPUT:
![Features Of Array In C](https://static.tutorialandexample.com/c/features-of-array-in-c5.png)
6. Fixed Size Collection:
Once an array in C is declared, its size cannot be changed. It cannot be changed after compilation; it must be comprehended at that time. The following example illustrates the fixed-size attribute of the array.
Example:
#include <stdio.h>
int main()
{
int arr[4] = {1, 2, 3, 4};
printf("Size of Array before checking: %d\n",sizeof(arr) / sizeof(int));
arr[5];
printf("Size of Array after checking: %d",sizeof(arr) / sizeof(int));
return 0;
}
OUTPUT:
![Features Of Array In C](https://static.tutorialandexample.com/c/features-of-array-in-c6.png)
7. Relationship between an array name and a pointer:
Arrays and pointers are closely related in that nearly all operations on an array may be carried out using pointers. The name of the array itself contains the pointer to the first entry.
Example:
#include <stdio.h>
int main()
{
int arr[4] = { 1, 2, 3, 4 };
int* ptr = &arr[0];
printf("Pointer pointing to First Element: %p\n", ptr);
printf("Array Name: %p", arr);
return 0;
}
OUTPUT:
![Features Of Array In C](https://static.tutorialandexample.com/c/features-of-array-in-c7.png)
8. Dimensions of Array:
In C, an array may have one or more dimensions, such as a 1-D array, a 2-D array, a 3-D array, and so forth. Dimensions can have any number of them. In a multidimensional array, the total number of elements is equal to the sum of all the dimensions' sizes.
Example:
#include <stdio.h>
int main()
{
int array_2d[2][2] = { 2, 4, 6, 8 };
int array_3d[2][2][2] = { 2, 4, 6, 8, 10, 12, 14, 16 };
printf("2-Dimensional Array: ");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", array_2d[i][j]);
}
}
printf("\n3-Dimensional Array: ");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", array_3d[i][j][k]);
}
}
}
return 0;
}
OUTPUT:
![Features Of Array In C](https://static.tutorialandexample.com/c/features-of-array-in-c8.png)