定义变量时复杂的从右到左规则

发布于 2024-12-20 01:29:53 字数 440 浏览 3 评论 0原文

这是问题:
使用从右到左规则编写名为 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 技术交流群。

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

发布评论

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

评论(3

我ぃ本無心為│何有愛 2024-12-27 01:29:53

一次构建一个片段:

一个名为 fubar 的变量...

fubar

...它是一个指针...

*fubar

...指向一个函数...

(*fubar)()

...它需要一个指向一个 char...

(*fubar)(char *)

...并返回一个指针...

*(*fubar)(char *)

...指向 7 个元素的数组...

(*(*fubar)(char *))[7]

...其中每个元素都是一个指针...

*(*(*fubar)(char *))[7]

...指向 struct Sporcle< /代码>。

struct Sporcle *(*(*fubar)(char *))[7]

你的答案是不正确的 - 左边的东西(称为声明说明符)只能直接指定类型(像int这样的基本类型,struct、unionenum 或使用 typedef 定义的类型名称,可以选择使用 static 等存储类说明符进行修改和/或类似的类型说明符常量)。指针、数组和函数类型是通过修改声明的右侧(称为声明符)来构造的,方法是添加*[] 或 () 到它。

在这种情况下,声明说明符是struct Sporcle,其余部分是声明符。

Build it up a piece at a time:

A variable named fubar...

fubar

...that is a pointer...

*fubar

...to a function...

(*fubar)()

...that takes a pointer to a char...

(*fubar)(char *)

...and returns a pointer...

*(*fubar)(char *)

...to an array of 7 elements...

(*(*fubar)(char *))[7]

...where each element is a pointer...

*(*(*fubar)(char *))[7]

...to a struct Sporcle.

struct Sporcle *(*(*fubar)(char *))[7]

Your answer is incorrect - the thing on the left (called the declaration specifier) can only directly specify a type (a base type like int, a struct, union, enum or type name defined with typedef, optionally modified with a storage class specifier like static and/or a type specifier like const). 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.

别理我 2024-12-27 01:29:53

cdecl(1) 是你的朋友:

cdecl> declare fubar as pointer to function(pointer to char) returning pointer to array 7 of pointer to struct Sporcle
struct Sporcle *(*(*fubar)(char *))[7]

cdecl(1) is your friend:

cdecl> declare fubar as pointer to function(pointer to char) returning pointer to array 7 of pointer to struct Sporcle
struct Sporcle *(*(*fubar)(char *))[7]
你另情深 2024-12-27 01:29:53

我不会评论有效性,但我会提供一个提示:不要这样做。除非这是纯粹的智力练习,否则请重构该声明以使其不再那么令人困惑:

typedef Sporcle SevenSporcles[7];
SevenSporcles* (*fubar)(char*);

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:

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