Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array length with pointers

How in C++ get array length with pointers only ? I know that tab name is pointer to first element, but what next ?

like image 399
sasklacz Avatar asked Mar 17 '10 00:03

sasklacz


People also ask

How do you find the length of an array?

Using sizeof() function to Find Array Length in C++ Hence, if we simply divide the size of the array by the size acquired by each element of the same, we can get the total number of elements present in the array.

Can you use sizeof on a pointer?

The code calls sizeof() on a malloced pointer type, which always returns the wordsize/8. This can produce an unexpected result if the programmer intended to determine how much memory has been allocated. The use of sizeof() on a pointer can sometimes generate useful information.


1 Answers

You cannot do it. You need to pass the array length along with the array pointer, or you need to use a container object such as std::vector.

like image 118
Tyler McHenry Avatar answered Oct 01 '22 12:10

Tyler McHenry