![]() |
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. Example Input: Find 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,
Output The size of the array is: 8 Explanation: 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 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. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |