How to Find the Size of an Array Using Pointer to its First Element? Last Updated : 31 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In C++, arrays are plain old data types that do not have any associated functions to find their size. In this article, we will discuss how we can determine the size of the array in C++ using the pointer to its first element.ExampleInput: int arr[]={1,2,3,4,5}Output:Size of the array is 5Find the Size of an Array from a Pointer to its First ElementUnfortunately, there is no way we can find the size of an Array using only the pointer to its first element. Even if we try to use sizeof to that pointer, we will only get the size of the pointer itself but not the array.We can see that in this example, C++ // C++ program to illustrate that we cannot find the size of // the array only using the pointer to its first element #include <iostream> using namespace std; int main() { // array int arr[] = { 1, 2, 3, 4, 5 }; // pointer int* ptr = arr; int size = sizeof(ptr); cout << "The size of the array is: " << size << endl; return 0; } OutputThe size of the array is: 8Explanation: In 64-bit architectures, the size of pointers is uniform, typically being 8 bytes. This uniformity is dictated by the operating system, ensuring that pointers to different data types (such as int, float, and double) all occupy the same amount of memory. Consequently, regardless of the data type, a pointer will consistently be 8 bytes on a 64-bit system.However, if you still want to work only with the pointer, you'll need to ensure that the size of the array is available through another mechanism, such as storing it in a variable, use the inbuilt containers like std::vectors, std::arrays, etc. If you still need to use arrays, keep a variable that stores the size of the array. Comment More infoAdvertise with us Next Article How to Find the Size of an Array Using Pointer to its First Element? gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-data-types cpp-array cpp-sizeof CPP Examples +2 More Practice Tags : CPP Similar Reads How to find the sum of elements of an Array using STL in C++? Given an array arr[], find the sum of the elements of this array using STL in C++. Example: Input: arr[] = {1, 45, 54, 71, 76, 12} Output: 259 Input: arr[] = {1, 7, 5, 4, 6, 12} Output: 35 Approach: Sum can be found with the help of accumulate() function provided in STL. Syntax: accumulate(first_ind 1 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 Second Smallest Element in an Array in C++? In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element 3 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 C++ Program to Find the Second Largest Element in an Array In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the second largest element in an array in C++. Examples: Input: arr[] = {34, 5, 16, 14, 56, 7, 56} Output: 34 Explanation: The 3 min read C++ Program to Access Elements of an Array Using Pointer Prerequisites: Pointers in C++Array in C++ A Pointer is a variable that stores the memory location or address of an object or variable. In other words, pointers reference a memory location, and obtaining the value stored at that memory location is known as dereferencing the pointer. An Array is the 2 min read How to Find All Indexes of an Element in an Array in C++? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. In this article, we will learn how to find all indexes of a specific element in an array in C++. Example: Input: myArray = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4}; target = 3Output: The element 3 occurred at 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 Size of a Vector in C++? The size of a vector means the number of elements currently stored in the vector container. In this article, we will learn how to find the size of vector in C++.The easiest way to find the size of vector is by using vector size() function. Letâs take a look at a simple example:C++#include <bits/s 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