You can reliably determine the length of arrays in C using various methods. Doing so makes it easier to manage arrays dy­nam­i­cal­ly 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 in­for­ma­tion is crucial for accessing in­di­vid­ual elements in the array, tra­vers­ing it or per­form­ing ma­nip­u­la­tions. When you declare an array in C, the memory for the elements in the array is allocated in the RAM con­tigu­ous­ly (i.e., in a se­quen­tial manner without gaps). In C, there is no built-in function for de­ter­min­ing array length, so you have to determine it manually.

The most common methods are:

  • sizeof()
  • Pointer arith­metic
  • Loops

sizeof()

sizeof()is an operator in C. It de­ter­mines 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 rep­re­sents 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 ex­pres­sion 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 rep­re­sents 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 in­for­ma­tion, 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 arith­metic

Pointers them­selves don’t contain in­for­ma­tion about the size or length of an array. However, we can use pointer arith­metic 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 in­cre­ment­ed by 1. Since arr is an array, this means that the pointer will point to the next con­sec­u­tive array that is the same type as arr.

Example

The ex­pres­sion *(&arr + 1) - arr cal­cu­lates the dif­fer­ence 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 dif­fer­ence 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 de­ter­min­ing 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 in­cre­ments 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
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support
Go to Main Menu