开发者

How do I declare a pointer to a pointer to an array?

I have a pointer to an array of Ts:

T (*anArray)[]; // A pointer to an array of Ts.

How do I declare a pointer to a pointer to an array of Ts? (ie. a pointer to the above.)

Is it:

T (**anArray)[];

or

开发者_StackOverflow中文版
T *(*anArray)[];

or

T (*anArray)*[];

or something else entirely?

Thanks,

Alf


The first one - T(**anArray)[];


The answer to your question is that the extra pointers outside of the parenthesis are applied to the type contained in the array, while inside the parenthesis they are applied to the type of the variable itself:

int (*array)[10];  // pointer to array of 10 int
int *(*array)[10]; // pointer to array of 10 pointer to int
int (**array)[10]; // pointer to pointer to array of 10 int

But the best advice is to avoid the problem and use typedefs:

typedef int array_t[10];
array_t **variable;      // pointer to pointer to array of 10 integers


T *(*anArray)[]; -> anArray is pointer to array of T pointers

T (**anArray)[]; -> anArray is pointer to pointer to array of T


Open cdecl.org and then copy-paste the following to the textbox:

  • declare a as pointer to pointer to array 10 of int

On pasting, it tells you what it means in C++. Well it is this:

  • int (**a)[10]

I'm sure that is what you're looking for? Play around with the link more. You will learn more syntaxes, and how to say that in words.


A good way of figuring out how to make unusual combinations of pointers like this is as follows: start with the variable name, look right without crossing over parentheses, look left, repeat. Say what you see in the order that you see it.

For a pointer to a pointer to an array we have

int (**a)[5]

Start with a. We look right, but there's a paren there, so look left. Ok we see "*", so it's a pointer. Now we look right again... still a paren, so back to the left. Another "*", so it's a pointer to a pointer. Look right, paren, look left, paren, so now we can jump out of the parentheses. Now we see brackets, so we have a pointer to a pointer to an array. Finally, look all the way to the left and we see int. Pointer to a pointer to an array of ints.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜