How to Find the Length of an Array in C++? Last Updated : 29 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the length of an array is defined as the total number of elements present in the array. In this article, we will learn how to find the length of an array in C++.The simplest way to find the length of an array is by using the sizeof operator. First calculate the size of the array in bytes and divide it by the size of the first element to get the length of the array. Let's take a look at an example: C++ #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; // Finding the length of the array int n = sizeof(arr) / sizeof(arr[0]); cout << n; return 0; } Output5Note: Any of the method mentioned here cannot find the size of those array that are passed to the functions due to array decay in C++. That is why it is recommended to store and pass the size of the array using some variable.Using Pointer ArithmeticFirst get the pointer to the array by using & addressof operator on the array name and increment this pointer. Deference the obtained pointer and subtract the pointer to the first element of the array to get the size of the array. C++ #include <bits/stdc++.h> using namespace std; int main() { int arr[5] = {1, 2, 3, 4, 5}; // Finding length of an array by using // pointer arithmetic cout << *(&arr + 1) - arr; return 0; } Output5Explanation: This trick works because of pointer arithmetic in C++. When an integer is added to a pointer, the compiler adjusts the address by multiplying the integer by the size of the data type the pointer points to.In this case, we took a pointer to a given array arr and added 1 to it. When we add 1 to a pointer of an array, the compiler moves the pointer by the size of one whole array (not element). Subtracting the pointer to the first element from this pointer gives the array's length.Using size() (Since C++17)C++17 introduced the size() function, which can be used to get the number of elements in an array. C++ #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; // Finding the size of array arr int n = size(arr); cout << n; return 0; } Output5 Comment More infoAdvertise with us Next Article How to Find the Length of an Array in C++? A akshitsaxena2 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to Find the Length of a Substring in a char Array? In C++, a substring is a string inside another string that is the contiguous part of the string. In this article, we will learn how to find the length of a substring in a char array in C++. Example Input: str[] = "Hello, World!";substr= "World";Output: Length of substring World is: 5Finding Length o 2 min read How to Find the Median of a Sorted Array in C++? In C++, the median of a sorted array is the middle element if the array size is odd, or the average of the two middle elements if the array size is even. In this article, we will learn how to find the median of a sorted array in C++. Example Input: myArray: {1, 2, 3, 4, 5} Output: Median of the Arra 2 min read How to Find the Sum of Elements in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O 2 min read How to Find the Size of a List in C++? In C++, Standard Template Library (STL) we have a std::list container that is a doubly-linked list in which elements are stored in non-contiguous memory allocation. In this article, we will learn how to find the size of a list in C++ STL. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Size o 2 min read How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma 2 min read How to Find the Index of an Element in an Array in C++? Given an array of n elements, the task is to find the index of a specific element in C++.ExamplesInput: arr[] = {11, 13, 9, 21, 51, 1000}, val = 9Output: 2Explanation: As the value 9 is present at index 2.Input: arr[] = {5, 8, 12, 9, 11, 32}, val = 11Output: 4Explanation: As the value 11 is present 3 min read How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Find the Size of a Set in Bytes in C++? In C++, sets are associative containers that only store unique elements. The elements inside the set are sorted in a specific order. In this article, we will learn how we can find the size of a set in bytes in C++. Example Input: S = {1,2,3,4}Output: Size of the set in bytes is : 16Find the Size of 2 min read How to Find the Size of a Set in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. No duplicate elements are allowed in the sets, as the value of every element in a set is unique. In this article, we will learn how we can find the size of a set container in C++. Example: Input: mySet={1 2 min read Like