Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: what does "T const *[]" mean?

Tags:

c++

  • what does "T const *[]" as parameter type mean?
  • What's the difference compared to "T *[]"?
  • And as last question: why can't I pass a "T *[]" to a function that requires a "T const * []" as parameter?

Thank you for your help.

Tobias

like image 761
Tobias Langner Avatar asked Apr 08 '11 12:04

Tobias Langner


1 Answers

As a type in general, it's an array of pointers to a constant T. Try putting a name in it:

T const *x[];

and apply the usual rules: [] binds tighter than *, so it's an array. Then the * means that its an array of pointers, and finally, they all point to a constant T. (As usual, const modifies whatever is to the left of it.)

As a parameter, of course, an array type is converted to a pointer type, so we end up with a pointer to a pointer to a constant T. This could also be written:

T const **

If you drop the const, you end up with:

T **

which is not the same thing as T const**.

And the reason you can't pass a T** or a T*[] to the first form is to prevent things like:

void f(int const* p1[]; int const* p2)
{
    p1[0] = *p2;
}
int const x = 10;
int* px[1];
f(px, &x);
*px[0] = 20;       //  Where does this write?

The fact that the declarations are written using [] is, in this case, misleading, since the rules for pointers to pointers still apply.

like image 53
James Kanze Avatar answered Oct 11 '22 04:10

James Kanze