1

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.

5
  • 2
    "How do you get the size of the array if you have a pointer to it?" You don't. If you have a pointer to an array, but don't know the size you are SOL. That is why you avoid using raw arrays, and use std::vector or std::array, since they carry around that information with them Commented Dec 5, 2014 at 14:32
  • Arrays decay into pointers if you so much as look at them weird. Commented Dec 5, 2014 at 14:32
  • You don't have to put the size [5] there, [] is enough. And the "5" is just documentation for the person reading the code. Commented Dec 5, 2014 at 14:33
  • 6
    @gnasher729 No it isn't. int (*array)[5] and int (*array)[] are not the same thing Commented Dec 5, 2014 at 14:34
  • By using std::array, you solve the common problems with C-Style arrays, yet keep their advantages. Well, in a nutshell at least. Commented Dec 5, 2014 at 15:17

4 Answers 4

3

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.)

Sign up to request clarification or add additional context in comments.

Comments

1
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.

Comments

1

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.

1 Comment

Yes, but that approach is pretty useless since you have to hardcode the size. And in the case where you have VLAs you have to pass the size anyway.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.