简体   繁体   English

C ++星号和括号运算符一起使用

[英]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 *x[]因为:

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: 使用cdecl ,您可以轻松确定类型:

declare x as pointer to array of int 将x声明为int数组的指针

So you can initialize x with the address of an array: 所以你可以用数组的地址初始化x

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) 请注意,数组的大小是其类型的一部分,因此您不能将大小为N的数组的地址分配给大小为M数组的指针,其中N!=M (这是上述错误的原因:您需要知道数组的大小来声明它)

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. int (*x)[]用于指向空int数组的指针。 But initialize an empty array not possible in c++. 但是初始化一个在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. int (*x)[]表示x是指向int数组的指针。 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. 表示x是一个数组,数组中的元素类型为int *即指向int的指针。 Thus x represents an array whose elements are int pointer. 因此, x表示一个数组,其元素是int指针。 However, the parentheses change precedence of the operator. 但是,括号会更改运算符的优先级。 Now * is first interpreted indicating x is a pointer, whose type is int [] . 现在*首先被解释为x是一个指针,其类型为int [] Thus, x in int (*x)[] is a pointer to int array and you should initialize it accordingly. 因此, xint (*x)[]是指向int数组和应该相应地进行初始化。

I used to have issues with C typing too, until I learned how it was created. 我曾经也遇到过C类型的问题,直到我了解它是如何创建的。

In C, the type is described in the way you would use a variable of that type. 在C中,类型以您使用该类型变量的方式描述。

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. 这意味着表达式*x的类型为int ,因此x是指向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. 这意味着表达式x[3]的类型为int ,因此x是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). 这意味着表达式*x的类型为int[] (即,未知大小的int数组)。 Therefore x is a variable of type pointer-to-an-array-of-int. 因此, x是指向数组指针的类型的变量。

int (*x)[ ] This means x is a pointer to an int array Initialize as: int (*x)[ ]; int(* x)[]这意味着x是一个指向int数组的指针Initialize as:int(* x)[]; int a[10]; int a [10]; x = &a; x =&a;

int *x[ ] This means x is an array of pointers to int int *x[10]; int * x []这意味着x是一个指向int int * x [10]的指针数组; int a[10]; int a [10]; x[0] = a; x [0] = a;

In my opinion, always use initialized array variables or use int **x instead of *x[ ] and then allocate memory on runtime. 在我看来,总是使用初始化的数组变量或使用int ** x而不是* x []然后在运行时分配内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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