Table of Contents
Properties of Array in C
In C, an array is a fixed-size collection of elements stored in a continuous block of memory. Arrays are a fundamental data type in C, capable of storing elements of various data types like integers, characters, or even custom structures. They are widely used by programmers to address various challenges not only in C but also in other programming languages.
The characteristics and behaviours of arrays can vary depending on the programming language. In this article, we will explore the specific properties and features of arrays 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 set when it’s declared and cannot be changed during program execution. This size must be determined at compile-time and remains constant.
// 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;
}
Output
Size of Array Before: 5
Size of Array After: 5
2. Homogeneous Collection
An array in C doesn’t have elements of different data types, instead they are of the same type.
Example
// 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] = "skill vertex";
// 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] = "skillvertex";
| ^
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
In C, indexing of elements in an array starts with 0, not 1. This means that the first element in the array has an index of 0, and the index of the last element is equal to the size of the array minus 1. So, if you have an array of size n
, the valid indices for that array range from 0 to n - 1
.
Example
// 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;
}
Output
Array[1]: 20
Array[0]: 10
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
In C, arrays can come in various forms. They can be single-dimensional, like 1-D arrays, or multidimensional, like 2-D, 3-D arrays, and beyond. These multidimensional arrays can have any number of dimensions, allowing you to represent complex data structures.
The number of elements in a multidimensional array is calculated by multiplying the size of all its dimensions. For example, in a 2-D array with dimensions m
and n
, the total number of elements is m * n
. In a 3-D array with dimensions x
, y
, and z
, the total number of elements is x * y * z
, and so on for higher dimensions.
This flexibility in array dimensions allows C programmers to work with a wide range of data structures and efficiently manage multi-dimensional data.
Example
// 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;
}
Output
2D Array: 1 2 3 4
3D Array: 1 2 3 4 5 6 7 8
5. Contiguous Storage
In C, all the elements in an array are stored in contiguous or consecutive memory locations. This concept is easy to visualize in the case of a 1-D array, where elements are simply placed one after the other in memory. However, it’s important to note that even multidimensional arrays are stored contiguously in memory.
The contiguous storage of multidimensional arrays is achieved by using row-major or column-major order. In row-major order, the elements of each row are stored one after another in memory, followed by the next row, and so on. In column-major order, the elements of each column are stored consecutively.
You can verify this contiguous storage property of arrays in C by using pointers to access and iterate through the elements in memory. By incrementing a pointer to an array element, you can observe that it points to the next consecutive element in memory, confirming the contiguous storage arrangement.
// 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;
}
Output
Address of arr[1] : 0x7fffb8cc1ef4
Address of arr[2] : 0x7fffb8cc1ef8
In the example you provided, where the difference between the addresses of arr[1]
and arr[2]
is 4 bytes, it indeed indicates that each element in the array occupies 4 bytes of memory. This aligns with the memory requirement for storing a single integer in many C implementations.
So, in memory addresses ranging from 0x7ffebc02e054
to 0x7ffebc02e057
, the element arr[1]
is stored, and in the subsequent 4 bytes of memory (0x7ffebc02e058
to 0x7ffebc02e05B
), the element arr[2]
is stored. This pattern continues for all the elements in the array, with each element occupying 4 bytes of memory, assuming it’s an integer array.
This alignment and contiguous storage of elements in memory are crucial for efficient array access and manipulation in C.
6. Random Access to the Elements
One of the defining and powerful properties of an array in C is the ability to access any element ra ndomly using its index. This capability is a direct consequence of contiguous storage, where all elements are stored consecutively in memory.
The compiler can determine the address of an element at a given index by utilizing the address of the first element and the index number. This calculation allows for efficient and direct access to any array element without needing to traverse through other elements. This random access property is fundamental for various algorithms and data structures that rely on quick and direct element retrieval.
Address of ith = Address of 1st Element + (Index * Size of Each Element)
Example
// 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;
}
Output
Array[3]: 4
Array[3] using pointer to first element = 4
7. Relationship between Array and Pointers
Arrays in C are closely related to pointers, and it’s true that many operations that can be performed on an array can also be accomplished using pointers. The name of an array is, in fact, a pointer to its first element. This relationship allows you to use pointer arithmetic to navigate and manipulate array elements.
// 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;
}
Output
Pointer to First Element: 0x7ffec5059660
Arran Name: 0x7ffec5059660
8. Bound Checking
In C, bound checking, which involves verifying whether the element being accessed falls within the declared range of the array, is not automatically performed by the language itself. This means that C allows you to access elements outside the specified range of the array without raising any runtime errors or exceptions.
While this flexibility can be useful in certain situations, it also comes with risks. Accessing elements outside the bounds of an array can lead to unexpected behavior and errors in your program, including memory corruption and security vulnerabilities. It’s the responsibility of the C programmer to ensure that array bounds are not violated and to perform manual bound checking when necessary to prevent these issues.
// 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;
}
Output
Some Garbage Value: 0
9. Array Decay
Array decay is the phenomenon where an array loses its dimension and turns into a pointer under certain circumstances. One common situation where array decay occurs is when an array is passed as an argument to a function. When an array decays into a pointer, you can no longer determine its size using the sizeof()
operator because the size information is lost.
// 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;
}
Output
Sizeof Value in Main: 3
Sizeof Value in Function: 8
The size of the array in the main() is 3 bytes which will be theb actual size of the array but when we check the size of the array in func(), the size comes out to be 8 bytes .So, instead of being the size of the array, it is the size of the pointer to the first element of the array.
FAQ- Properties of Array in C
Q1. What is array and its types in C?
Ans. Arrays store multiple values in a single variable. To create an array, specify the data type (e.g., int) and give it a name followed by square brackets [].
Q2. What are types of arrays?
Ans. Arrays are categorized into two types: single-dimensional and multi-dimensional. A single-dimensional array represents linear data, while a two-dimensional array represents a matrix. Multidimensional arrays have multiple dimensions.
Q3. What is the syntax of array?
Ans. Arrays are defined using square brackets with the size specified:
1D Arrays: int arr[n];
2D Arrays: int arr[m][n];
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together