我可以将结构指针转换为指向 C1x 中的初始匿名成员吗?这是正确的问题吗?

发布于 2024-11-27 13:07:40 字数 947 浏览 1 评论 0原文

我对 C1x 中的匿名结构有点困惑。适当转换的结构指针指向其第一个成员的规则是否适用于初始匿名结构,或者仅适用于初始匿名结构的初始成员?特别是,这个程序在 C1x 中有意义吗?

#include<stdio.h>

struct node {
    struct node *next;
};

/* Does C1x even allow this? Do I have to define struct node inside of inode?
 * Are anonymous struct members even allowed to have tags?
 */
struct inode {
    struct node;
    int data;
};

int main(void) {
    inode node1 = {NULL, 12};
    inode *ihead = &inode;
    node *head = (struct node *)ihead;

    /* These should work since struct inode's first member is a struct node. */
    printf("Are these equal? %c", head == &node1.next ? 'Y' : 'N');
    printf("Is next NULL? %c", head->next == NULL ? 'Y' : 'N');

    return 0;
}

这个答案表明我可能会问关于未命名结构而不是匿名结构。我是否完全误解了匿名结构的本质?

I'm a bit confused about anonymous structures in C1x. Does the rule that a struct pointer, suitably converted, points to it's first member apply to an initial anonymous struct, or simply to the initial member of an initial anonymous struct? In particular, does this program make sense in C1x?

#include<stdio.h>

struct node {
    struct node *next;
};

/* Does C1x even allow this? Do I have to define struct node inside of inode?
 * Are anonymous struct members even allowed to have tags?
 */
struct inode {
    struct node;
    int data;
};

int main(void) {
    inode node1 = {NULL, 12};
    inode *ihead = &inode;
    node *head = (struct node *)ihead;

    /* These should work since struct inode's first member is a struct node. */
    printf("Are these equal? %c", head == &node1.next ? 'Y' : 'N');
    printf("Is next NULL? %c", head->next == NULL ? 'Y' : 'N');

    return 0;
}

This answer suggests that I may be asking about unnamed structs instead of anonymous structs. Am I completely misunderstanding the nature of anonymous structs?

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

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

发布评论

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

评论(2

捎一片雪花 2024-12-04 13:07:40
/* Does C1x even allow this? Do I have to define struct node inside of inode?
 * Are anonymous struct members even allowed to have tags?
 */
struct inode {
    struct node;
    int data;
};

首先,struct node 成员不是匿名。它是未命名的,但因为它有一个标签(node),所以它不满足C1x对“匿名”的定义。

除此之外,C1x 语法显然允许这样做。由于 SO 并不真正支持下标,因此我用方括号标记了可选元素;我还省略了一些语法规则,这些规则对于确保这是有效的没有必要:

type-specifier:
    struct-or-union-specifier
struct-or-union-specifier:
    struct-or-union [identifier] { struct-declaration-list }
struct-or-union:
    struct
    union
struct-declaration-list:
    struct-declaration
    struct-declaration-list struct-declaration
struct-declaration:
    specifier-qualifier-list [struct-declarator-list];
specifier-qualifier-list:
    type-specifier [specifier-qualifier-list]

还有 4 个约束需要满足,但它们都不适用于未命名的成员,因此这在语法上是有效的 C。

现在,转到您的真正的问题。 C1x 说:

指向结构对象的指针,经过适当转换后,指向其初始成员 (...),反之亦然。

句号。没有“除非该成员未透露姓名”。因此,指向node1的指针也是指向未命名初始成员的指针,也是指向node1.next的指针。然而,这里开始变得有点毛茸茸的。可能我只是忽略了一个条款,但 C1x 似乎只说了:

匿名结构或联合的成员被视为包含结构或联合的成员。

我找不到语言说未命名结构的成员被视为包含结构的成员。实际上,除了指针双关之外,没有任何有保证的方法可以访问 next。我将写信给委员会的一些人要求澄清这一点。

您还可能会遇到初始化问题:

结构对象的未命名成员即使在初始化后也具有不确定的值。

/* Does C1x even allow this? Do I have to define struct node inside of inode?
 * Are anonymous struct members even allowed to have tags?
 */
struct inode {
    struct node;
    int data;
};

First, the struct node member is not anonymous. It is unnamed, but because it has a tag (node), it does not satisfy the C1x definition of "anonymous".

That aside, this is clearly allowed by the C1x grammar. Since SO doesn't really support subscripts, I've marked optional elements with square brackets; I've also elided grammar rules that are not necessary to see that this is valid:

type-specifier:
    struct-or-union-specifier
struct-or-union-specifier:
    struct-or-union [identifier] { struct-declaration-list }
struct-or-union:
    struct
    union
struct-declaration-list:
    struct-declaration
    struct-declaration-list struct-declaration
struct-declaration:
    specifier-qualifier-list [struct-declarator-list];
specifier-qualifier-list:
    type-specifier [specifier-qualifier-list]

There are also 4 constraints that need to be satisfied, but none of them apply to unnamed members, so this is syntactically valid C.

Now, on to your real question. C1x says:

A pointer to a structure object, suitably converted, points to its initial member (...), and vice versa.

Full-stop. No "unless that member is unnamed". So a pointer to node1 is also a pointer to the unnamed initial member, is also a pointer to node1.next. However, here it starts to get a little wooly. It could be that I've simply overlooked a clause, but it seems that C1x only says that:

The members of an anonymous structure or union are considered to be members of the containing structure or union.

I cannot find language saying that members of an unnamed structure are considered to be members of the containing structure. It could actually be that there is no guaranteed way to access next other than pointer-punning. I will write to some people on the committee to ask for clarification on this point.

You may also run into trouble with initialization:

Unnamed members of structure objects have indeterminate value even after initialization.

泪痕残 2024-12-04 13:07:40

尝试使用节点作为类型名称。避免再次声明“结构节点”。

struct inode {
    node n;//node is a already a defined type name, n is the var name
    int data;
};

Try using node as a type name. Avoid declaring again "struct node".

struct inode {
    node n;//node is a already a defined type name, n is the var name
    int data;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文