c++ - int(*a)的解释[3]

标签 c++ c arrays pointers types

在 C 语言中使用数组和指针时,人们很快就会发现它们绝不是等价的,尽管乍一看似乎是等价的。我知道 L 值和 R 值的差异。不过,最近我试图找出可以与二维数组结合使用的指针类型,即

int foo[2][3];
int (*a)[3] = foo;

但是,尽管 *[] 有常规的运算符优先级规则,但我就是不知道编译器如何“理解” a 的类型定义.相反,如果我使用 typedef,问题就会变得简单得多:

int foo[2][3];
typedef int my_t[3];
my_t *a = foo;

最后,有人可以回答我关于编译器如何读取术语 int (*a)[3] 的问题吗? int a[3] 很简单,int *a[3] 也很简单。但是,为什么不是 int *(a[3])

编辑:当然,我指的是“typedef”而不是“typecast”(这只是一个错字)。

最佳答案

每当您对复杂的声明有疑问时,可以使用 cdecl类 Unix 系统中的工具:

[/tmp]$ cdecl
Type `help' or `?' for help
cdecl> explain int (*a)[10];
declare a as pointer to array 10 of int

编辑:

此工具还有一个在线版本here .

感谢 Tiberiu Ana 和 gf

关于c++ - int(*a)的解释[3],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2250397/

相关文章:

c - C中的位操作

javascript - html5 mustache获取文章索引并比较值

mysql - 无法将 Time 对象保存到 Ruby on Rails 中的 Time 列数组中

c - 在linux上使用c保存bmp文件

函数调用中未填充 C 字符串

java - Android 上的数组到字符串

c++在程序中使用私有(private)类数据

c++ - 在 C++ 中解析字符串,使用 char 数据类型

c++ - 使用 libsox 将音频转换为 FLAC

c++ - CMakeLists.txt : (add_executable): No SOURCES given to target: Tutorial