how to find size of int array in c++
To find the size of an int array in C++, you can use the sizeof
operator. Here are the steps to achieve this:
- Declare and initialize your int array.
- Use the
sizeof
operator to determine the size of the array. - Divide the total size of the array by the size of a single element to get the number of elements in the array.
Here is an example:
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
In this example, we declare and initialize an int array called arr
with 5 elements. We then use the sizeof
operator to determine the total size of the array in bytes. We divide this by the size of a single element, sizeof(arr[0])
, which gives us the number of elements in the array.
The result, stored in the size
variable, will be 5, indicating that the array arr
has 5 elements.
By using this method, you can find the size of any int array in C++.