How to determine the length of an array in C
You can reliably determine the length of arrays in C using various methods. Doing so makes it easier to manage arrays dynamically and adapt to changes in size.
What is array length in C and what methods are there for measuring it?
When measuring the length of an array in C, you are measuring the number of elements the array contains. This information is crucial for accessing individual elements in the array, traversing it or performing manipulations. When you declare an array in C, the memory for the elements in the array is allocated in the RAM contiguously (i.e., in a sequential manner without gaps). In C, there is no built-in function for determining array length, so you have to determine it manually.
The most common methods are:
sizeof()
- Pointer arithmetic
- Loops
sizeof()
sizeof()
is an operator in C. It determines the size of a data type or a variable in bytes during the compile time. The return type of the sizeof
operator is size_t
, an unsigned integer type that represents the size in bytes. In C, you can also use the sizeof()
function to calculate array lengths.
Syntax
To determine the number of elements in an array, you must divide the total size of the array by the size of a single element.
- data_type: This is the data type where the length of the array will be stored.
- array_name: Specifies the name of the array.
- sizeof(array_name): This expression statement returns the total size of the array in bytes.
- sizeof(array_name[index]): By dividing the total size of the array by the size of a single element, you get the number of elements in the array.
- index: This represents the index of an element in the array.
Example
In this example, we’re going to use sizeof()
to determine the size of the array myArray
as well as the size of a single element. With this information, we can then calculate the number of elements in the array by dividing the total size by the size of a single element. We are going to measure the size of the element and the array in bytes.
The output is:
Pointer arithmetic
Pointers themselves don’t contain information about the size or length of an array. However, we can use pointer arithmetic together with the addresses of array elements to determine the C array length.
Syntax
&arr
: This creates a pointer that points to an entire array of elements.(&arr + 1)
: Here, the pointer that is pointing to the arrayarr
is incremented by 1. Sincearr
is an array, this means that the pointer will point to the next consecutive array that is the same type asarr
.
Example
The expression *(&arr + 1) - arr
calculates the difference between the pointer that points to the next array &arr + 1
and the pointer that points to the first element of the original array. In this case, the “next” memory area is the end of the first array. The resulting difference is equal to the number of elements in the array.
Output:
For loops
Another method for determining array lengths in C is to use a for loop. This approach involves iterating through the array and counting the number of elements it contains. To use this technique, the array whose length you want to determine needs to be in the same scope of code as the for loop you are using to determine its length.
Example
Here, the loop increments the arrayLength
counter for each element that is passed through. The i < sizeof(arr) / sizeof(arr[0])
condition ensures that the loop only runs if elements are in the array.
Output:
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups