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 arrayLength = sizeof(array_name) / sizeof(array_name[index]);
c
  • 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.

int main() {
    int myArray[] = {1, 2, 3, 4, 5};
    size_t totalSize = sizeof(myArray);
    size_t elementSize = sizeof(myArray[0]);
    size_t arrayLength = totalSize / elementSize;
    printf("Total size of the array: %d bytes\n", (int)totalSize);
    printf("Size of a single element: %d bytes\n", (int)elementSize);
    printf("Number of elements in the array: %d\n", (int)arrayLength);
    return 0;
}
c

The output is:

Total size of the array: 20 bytes
Size of a single element: 4 bytes
Number of elements in the array: 5
c

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

data_type arr_length = *(&arr + 1) - arr;
c
  • &arr: This creates a pointer that points to an entire array of elements.
  • (&arr + 1): Here, the pointer that is pointing to the array arr is incremented by 1. Since arr is an array, this means that the pointer will point to the next consecutive array that is the same type as arr.

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.

#include <stdio.h>
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int length = *(&arr + 1) - arr;
    printf("Number of elements in the array: %d\n", length);
    return 0;
}
c

Output:

Number of elements in the array: 5
c

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.

#include <stdio.h>
int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
    size_t arrayLength = 0;
    for (size_t i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i) {
        arrayLength++;
    }
    printf("Number of elements in the array: %d\n", arrayLength);
    return 0;
}
c

Output:

Number of elements in the array: 8
c
Web Hosting
Fast, scalable hosting for any website
  • 99.9% uptime
  • PHP 8.3 with JIT compiler
  • SSL, DDoS protection, and backups
Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top