我可以将结构指针转换为指向 C1x 中的初始匿名成员吗?这是正确的问题吗?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,
struct node
成员不是匿名。它是未命名的,但因为它有一个标签(node
),所以它不满足C1x对“匿名”的定义。除此之外,C1x 语法显然允许这样做。由于 SO 并不真正支持下标,因此我用方括号标记了可选元素;我还省略了一些语法规则,这些规则对于确保这是有效的没有必要:
还有 4 个约束需要满足,但它们都不适用于未命名的成员,因此这在语法上是有效的 C。
现在,转到您的真正的问题。 C1x 说:
句号。没有“除非该成员未透露姓名”。因此,指向node1的指针也是指向未命名初始成员的指针,也是指向node1.next的指针。然而,这里开始变得有点毛茸茸的。可能我只是忽略了一个条款,但 C1x 似乎只说了:
我找不到语言说未命名结构的成员被视为包含结构的成员。实际上,除了指针双关之外,没有任何有保证的方法可以访问
next
。我将写信给委员会的一些人要求澄清这一点。您还可能会遇到初始化问题:
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:
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:
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 tonode1.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: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:
尝试使用节点作为类型名称。避免再次声明“结构节点”。
Try using node as a type name. Avoid declaring again "struct node".