user4329091
user4329091

Reputation: 23

How do you get size of an array by using pointer to array?

I have read this answer

Adressing your question - pointer to array is usefull to pass an entire array of compile-time known size and preserve information about its size during argument passing.

But i don't really understand it. Aren't the size of arrays with a given size already known at compile-time? How do you get the size of the array if you have a pointer to it? Take this example:

void func(int (*array)[5])
{

}

// identical to
void func(int *array, int size)
{

}

You have to put 5 there, so what's the point of it? You still can't iterate over it unless you already know the size.

Upvotes: 1

Views: 142

Answers (4)

Jexus
Jexus

Reputation: 473

if you have a pointer p point to the array
and you want to get the array size.
try size_t array_size = *(size_t*)p;
Dangerous. But it works.

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254431

Aren't the size of arrays with a given size already known at compile-time?

Yes, they are.

How do you get the size of the array if you have a pointer to it?

You don't.

You have to put 5 there, so what's the point of it?

It prevents mistakes. You can only pass an array of the correct size to this function; the compiler will reject it if you try to pass a pointer, or wrongly sized array.

You still can't iterate over it unless you already know the size.

You can get the size from the array type:

size_t size = sizeof(*array) / sizeof(**array);      // old school
size_t size = std::extent<decltype(*array)>::value;  // C++11 or later
size_t size = std::size(*array);                     // the future, maybe

Or you could make the function a template, usable for any array size:

template <size_t N>
void func(int (&array)[N])
{
    for (int i : array) // size is known
       std::cout << i << '\n';
}

(I also changed the type to a reference rather than a pointer, to make the syntax clearer. It's possible that the answer you quote was for C, not for C++, in which case there are no references or templates.)

Upvotes: 3

user4329091
user4329091

Reputation: 23

Mike Seymour's answer with the template example has made it click to me that you can use sizeof operator here.

void func(int (*array)[5])
{
    std::size_t n = sizeof(*array) / sizeof(**array);
    std::cout << n;
}

int main()
{
    int array[5] = { 1, 2, 3, 4, 5 };
    func(&array);
}

This approach works best in C, where you don't have templates.

Upvotes: 1

ravi
ravi

Reputation: 10733

Adressing your question - pointer to array is useful to pass an 
entire array of compile-time known size and preserve information
 about its size during argument passing.

This is just true for char arrays as you don't need to pass size of array explicitly since its deduced by the null terminator.

When it comes to integer arrays (OR arrays where there is no terminator), I would say that they are not self-contained as passing pointer to array won't let that function to deduce the size of array. You have to pass size explicitly.

Upvotes: 1

Related Questions