how to find the length of an array in cpp

Use the following code to find the length of an array in C++:

int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]);

Explanation: - The array arr contains the elements 1, 2, 3, 4, and 5. - sizeof(arr) returns the total size of the array in bytes. - sizeof(arr[0]) returns the size of the first element in bytes. - Dividing the total size of the array by the size of the first element gives the number of elements in the array, which is assigned to the variable length.