Using Pointer Arithmetic
Pointer arithmetic is another technique that can be used to find the length of an array in C. In C, an array name is essentially a pointer to the first element of the array. By subtracting the pointer to the first element from the pointer to the element beyond the last element, we can determine the length of the array.
Here's the syntax to find the length of an array using pointer arithmetic:
int length = &array[size] - array;
Let's understand how this works:
- array represents the pointer to the first element of the array.
- &array[size] represents the pointer to the element beyond the last element of the array, where size is the total number of elements in the array.
- Subtracting the pointer to the first element from the pointer to the element beyond the last element gives us the length of the array.
Example
C
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
int length = &str[sizeof(str)] - str;
printf("Length of the string: %d\n", length);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Length of the string: 13
In this example, we have a character array str containing the string "Hello, World!". By subtracting the pointer to the first element (str) from the pointer to the element beyond the last element (&str[sizeof(str)]), we calculate the length of the array. The resulting length includes the null terminator character ('\0') at the end of the string.
Pointer arithmetic provides a low-level approach to finding the length of an array. It relies on the relationship between pointers & array elements. However, it's important to note that this method may not work accurately if the array decays to a pointer in certain contexts, such as when passing an array to a function.
Using Loop
Another approach to finding the length of an array in C is by using a loop to iterate over the elements of the array until a specific condition is met. This method is particularly useful when the array contains a sentinel value or a terminating character that indicates the end of the array.
Example
C
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
int length = 0;
while (str[length] != '\0') {
length++;
}
printf("Length of the string: %d\n", length);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Length of the string: 13
In this example, we have a character array str containing the string "Hello, World!". We initialize a variable length to 0, which will keep track of the length of the string. We then use a while loop to iterate over the characters of the string until we encounter the null terminator character ('\0'). For each character, we increment the length variable.
By the end of the loop, the length variable will hold the length of the string, excluding the null terminator. The resulting length is then printed to the console.
Using a loop to find the length of an array is particularly useful when the array doesn't have a fixed size or when the size is not known beforehand. It allows you to dynamically determine the length based on the actual content of the array.
However, it's important to note that this method relies on the presence of a sentinel value or a terminating character to indicate the end of the array. If the array doesn't have such an indicator, using a loop may result in accessing elements beyond the array's bounds, leading to undefined behavior.
Frequently Asked Questions
Can I use the sizeof() operator to find the length of an array passed as a function argument?
No, the sizeof() operator will return the size of the pointer instead of the array when used inside a function.
Is it necessary to use a null terminator character when finding the length of a string using a loop?
Yes, the null terminator character ('\0') is used to mark the end of a string and is essential for determining its length using a loop.
What is the time complexity of finding the length of an array using the sizeof() operator or pointer arithmetic?
The time complexity of finding the length of an array using sizeof() or pointer arithmetic is O(1) since it involves a constant-time operation.
How to find the length of an array in C without using sizeof?
To find the length of an array in C without using sizeof, you can iterate through the array with a loop, counting the elements until you reach the array's end (null).
Conclusion
In this article, we learned three methods to find the length of an array in C: using the sizeof() operator, pointer arithmetic & loops. The sizeof() operator provides a simple way to calculate the length, while pointer arithmetic allows us to determine the length by subtracting pointers. Loops can be used to find the length of arrays with sentinel values or terminating characters. Learning all these techniques enables you to efficiently work with arrays in C.