“char (*a)[12]”是什么意思?意思是?

发布于 2024-09-05 00:59:35 字数 16 浏览 2 评论 0原文

这是来自C标准吗?

Is this from the C standard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别再吹冷风 2024-09-12 00:59:35

如果您对 C 声明感到困惑,可以使用 cdecl 程序来解释它:

~$ cdecl
Type `help' or `?' for help
cdecl> explain char (*a)[12];
declare a as pointer to array 12 of char

If you're confused by a C declaration, you can use the cdecl program to explain it:

~$ cdecl
Type `help' or `?' for help
cdecl> explain char (*a)[12];
declare a as pointer to array 12 of char
月下客 2024-09-12 00:59:35

因为 C 中的声明遵循运算符优先级规则(即在间接寻址之前评估数组订阅),所以您需要括号来声明指向数组类型的指针。

在许多用例中,与使用普通 char * 相比,实际上并没有任何实际好处,除了它是一种强制数组大小的方法,特别是当用作函数参数时:

void foo(char bar[42]);

相当于

void foo(char *bar);

并接受任何char *,而

void foo(char (*bar)[42]);

只接受指向大小为 42 的数组的指针。

由于在后一种情况下访问 bar 的元素很麻烦,因此最好在函数体中立即定义一个等效的 char *

char *baz = *bar;

以便您可以直接使用订阅 baz[13] 而不是 (*bar)[13]

Because declarations in C follow the operator precedence rules (ie array subscription is evaluated before indirection), you'll need parens to declare pointers to array types.

In many use cases, there's not really any practical benefit over using a plain char *, except that it's a way to enforce the array size, especially when used as a function parameter:

void foo(char bar[42]);

is equivalent to

void foo(char *bar);

and accepts any char *, whereas

void foo(char (*bar)[42]);

will only accept pointers to arrays of size 42.

As accessing the elements of bar in the latter case is cumbersome, it might be a good idea to immediately define an equivalent char * in the function body

char *baz = *bar;

so that you can use direct subscription baz[13] instead of (*bar)[13].

风蛊 2024-09-12 00:59:35

指向 12 个字符数组的指针。

A pointer to an array of 12 characters.

瀞厅☆埖开 2024-09-12 00:59:35

a 是一个指向 12 个字符的数组 的指针。

a is a pointer pointing to an array of 12 characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文