简体   繁体   English

(int (*)[30]) 中的 (*) 是什么意思?

[英]What does the (*) in (int (*)[30]) mean?

What does (int (*)[30]) mean in C? C中的(int (*)[30])是什么意思? For instance, in:例如,在:

int (*b)[30] = (int (*) [30]) malloc(30 * sizeof(int [20]));

It means, roughly, "is a pointer".粗略地说,它的意思是“是一个指针”。

int (*b)[30]

This means " b is a pointer to an array of 30 integers".这意味着“ b是指向 30 个整数的数组的指针”。

(int (*) [30])

This means "cast to a pointer to an array of 30 integers".这意味着“转换为指向 30 个整数数组的指针”。

int (*b)[30] = (int (*) [30]) malloc(30 * sizeof(int [20]));

Breaking it down:打破它:

      b        -- b
    (*b)       -- is a pointer
    (*b)[30]   -- to a 30-element array
int (*b)[30]   -- of int.

In both declarations and expressions, postfix operators like [] have higher precedence than unary operators like * , so T *a[] is interpreted as T *(a[]) ;在声明和表达式中,像[]这样的后缀运算符比像*这样的一元运算符具有更高的优先级,因此T *a[]被解释为T *(a[]) IOW, a is an array of pointer to T . IOW, a是指向T的指针数组。 To designate a as a pointer to an array , we have to force the grouping T (*a)[] .要将a指定为指向数组的指针,我们必须强制分组T (*a)[]

Simlilarly, the cast expression (int (*) [30]) means "treat the pointer value returned by malloc as a pointer to a 30-element array of int ".同样,强制转换表达式(int (*) [30])表示“将malloc返回的指针值视为指向 30 元素int数组的指针”。 Note that, technically speaking, the cast expression is superfluous and should be removed.请注意,从技术上讲,cast 表达式是多余的,应该删除。

The malloc call itself seems very wrong. malloc调用本身似乎非常错误。 You're allocating 30 instances of a 20-element array of int , but assigning the result to a pointer to a 30-element array of int ;您正在为int的 20 元素数组分配 30 个实例,但将结果分配给指向int的 30 元素数组的指针; that's going to cause problems.那会引起问题。 Assuming you're trying to allocate a N x 30 matrix of int , the following would be safer:假设您正在尝试分配一个 N x 30 矩阵int ,以下内容会更安全:

int (*b)[30] = malloc(N * sizeof *b); 

The type of the expression *b is int [30] , so sizeof *b is the same as sizeof (int [30]) .表达式*b的类型是int [30] ,因此sizeof *bsizeof (int [30])相同。

How to parse C declarations and types: unwind them from outside in.如何解析 C 声明和类型:从外向内展开它们。

  • int (*b)[30] . int (*b)[30]
  • (*b)[30] is an int . (*b)[30]是一个int
  • (*b) is an int array of length 30 . (*b)是一个长度为30int数组。
  • b is a pointer to an int array of length 30 . b是指向长度为30int数组的指针。

The nameless version int (*) [30] is entirely identical, just the name has been omitted.无名版本int (*) [30]完全相同,只是省略了名称。

If you have a copy of The C Programming Language, there's a program in there called cdecl that can transform such declarations into English.如果您有一份 C 编程语言,其中有一个名为cdecl的程序可以将此类声明转换为英语。 There's been various modifications of it over time, for example cutils in Debian supports the nameless form, and cdecl.org is online.随着时间的推移对其进行了各种修改,例如 Debian 中的cutils支持无名形式,并且cdecl.org在线。

You can use cdecl to figure these kinds of things out:您可以使用cdecl来解决这些问题:

cdecl> explain (int (*) [30]) 
cast unknown_name into pointer to array 30 of int

(*) before a variable name means that is a POINTER. (*) 在变量名前表示它是一个指针。

We have just seen that a variable which stores a reference to another variable is called a pointer.我们刚刚看到,存储对另一个变量的引用的变量称为指针。 Pointers are said to "point to" the variable whose reference they store.指针被称为“指向”它们存储其引用的变量。

Using a pointer we can directly access the value stored in the variable which it points to.使用指针,我们可以直接访问存储在它指向的变量中的值。 To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to "value pointed by".为此,我们只需要在指针的标识符之前加上一个星号 (*),它充当解引用运算符,可以直译为“指向的值”。

There's really nothing special about (*) . (*)真的没有什么特别的。 Since you're just referencing a type in your typecast (and not a variable, which has a name, of that type), you simply omit the name.由于您只是在类型转换中引用一个类型(而不是具有该类型名称的变量),因此您只需省略名称即可。 Here the parens are still needed to distinguish an array of pointers from a pointer to an array.这里仍然需要括号来区分指针数组和指向数组的指针。

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

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