简体   繁体   中英

C++ asterisk and bracket operators used together

Sorry for the crappy title, but I don't know how to better describe this problem.

What is the meaning of:

int (*x)[];

and how to initialize it?

I know it isn't int *x[] since:

int a,b;
int (*x)[] = {&a, &b};

won't compile.

Thank you in advance.

Type declarations have an expression-like syntax, so you parse them as you would an expression:

      x       x is
     *x       a pointer
    (*x)[]    to an array of unknown dimensions
int (*x)[]    of int

The precedence rule is that the operators to the right bind tighter than those to the left, in each case, the operator closer to the element binds tighter, and finally, that parentheses can be used to change these bindings, so:

int  *x[];    is an array of pointers,
int *(x[]);   as is this.
int (*x)[];   whereas here, the parentheses change the bindings.

Using cdecl , you can easily determine the type:

declare x as pointer to array of int

So you can initialize x with the address of an array:

int a[]; // conceptual: error if compiled
x = &a;

Note that the size of the array is part of its type, so you cannot assign the address of an array of size N to a pointer to array of size M , where N!=M (this is the reason for the error above: you need to know the size of the array to declare it)

int b[5];
int c[6];
int (*y)[6];
y = &b; // error
y = &c; // OK

int (*x)[] using for a pointer to an empty int array. But initialize an empty array not possible in c++. Only you can define it. You should use a non-empty array:

int a[2] = {1, 2};
int (*x)[2] = &a;

int (*x)[] means x is a pointer to int array. You can do like this:

int a[] = {1, 2};
int (* b)[2] = &a;

When interpreted array-typed variable, we should go from right to left. eg

int *x[]

indicates that x is an array, elements in the array is typed as int * ie pointer to int. Thus x represents an array whose elements are int pointer. However, the parentheses change precedence of the operator. Now * is first interpreted indicating x is a pointer, whose type is int [] . Thus, x in int (*x)[] is a pointer to int array and you should initialize it accordingly.

I used to have issues with C typing too, until I learned how it was created.

In C, the type is described in the way you would use a variable of that type.

Therefore, when you see:

int *x;

it means that the expression *x is of type int , so x is variable of type pointer-to-int.

And if you see:

int x[5];

it means that the expression x[3] is of type int , so x is a variable of type array-of-int.

So, to get to your expression:

int (*x)[];

it means that the expression *x is of type int[] (ie, an array of int of unknown size). Therefore x is a variable of type pointer-to-an-array-of-int.

int (*x)[ ] This means x is a pointer to an int array Initialize as: int (*x)[ ]; int a[10]; x = &a;

int *x[ ] This means x is an array of pointers to int int *x[10]; int a[10]; x[0] = a;

In my opinion, always use initialized array variables or use int **x instead of *x[ ] and then allocate memory on runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM