array length c++
The length of a C++ array can be obtained using the sizeof
operator. Here's an example:
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]);
In this example:
- Define an array of integers arr
with some values.
- Use sizeof(arr)
to get the total size of the array in bytes.
- Use sizeof(arr[0])
to get the size of a single element.
- Divide the total size of the array by the size of a single element to obtain the length of the array.