定义变量时复杂的从右到左规则
这是问题:
使用从右到左规则编写名为 fubar 的变量的 C 定义,该变量是指向函数的指针, 接受一个指向 char 的指针,并返回一个指向包含 7 个元素的数组的指针,其中每个元素都是指向 结构孢子。
我的答案:
*( (Sporcle*)[7] ) ( *fubar )( char* );
任何人都可以验证我的答案和/或给我一些指示(没有双关语)?
编辑答案:
( (struct Sporcle*)[7] ) *( *fubar )( char* );
最终答案
struct Sporcle *(*(*fubar)(char *))[7];
This is the question:
Using the Right-Left rule write the C definition of a variable named fubar that is a pointer to a function that
takes a pointer to a char and returns a pointer to an array of 7 elements where each element is a pointer to a
struct Sporcle.
My answer:
*( (Sporcle*)[7] ) ( *fubar )( char* );
Can anyone verify my answer and/or give me some pointers (no pun intended)?
Edited Answer:
( (struct Sporcle*)[7] ) *( *fubar )( char* );
Final Answer
struct Sporcle *(*(*fubar)(char *))[7];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一次构建一个片段:
一个名为
fubar
的变量......它是一个指针...
...指向一个函数...
...它需要一个指向一个 char...
...并返回一个指针...
...指向 7 个元素的数组...
...其中每个元素都是一个指针...
...指向
struct Sporcle< /代码>。
你的答案是不正确的 - 左边的东西(称为声明说明符)只能直接指定类型(像
int
这样的基本类型,struct、
union
、enum
或使用typedef
定义的类型名称,可以选择使用static
等存储类说明符进行修改和/或类似的类型说明符常量)。指针、数组和函数类型是通过修改声明的右侧(称为声明符)来构造的,方法是添加*
、[] 或
()
到它。在这种情况下,声明说明符是struct Sporcle,其余部分是声明符。
Build it up a piece at a time:
A variable named
fubar
......that is a pointer...
...to a function...
...that takes a pointer to a char...
...and returns a pointer...
...to an array of 7 elements...
...where each element is a pointer...
...to a
struct Sporcle
.Your answer is incorrect - the thing on the left (called the declaration specifier) can only directly specify a type (a base type like
int
, astruct
,union
,enum
or type name defined withtypedef
, optionally modified with a storage class specifier likestatic
and/or a type specifier likeconst
). Pointer, array and function types are constructed by modifying the right-hand-side of the declaration (called the declarator), by adding*
,[]
or()
to it.In this case, the declaration specifier is
struct Sporcle
and the remainder is the declarator.cdecl(1)
是你的朋友:cdecl(1)
is your friend:我不会评论有效性,但我会提供一个提示:不要这样做。除非这是纯粹的智力练习,否则请重构该声明以使其不再那么令人困惑:
I won't comment on the validity, but I will offer a pointer: don't do that. Unless this is a purely intellectual exercise, refactor the declaration to make it less of a puzzle: