【发布时间】:2011-08-03 09:29:26
【问题描述】:
我对 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;
}
This 的回答表明我可能在询问未命名的结构而不是匿名结构。我是否完全误解了匿名结构的性质?
【问题讨论】:
-
当我再次关闭手机时,我会尝试查找答案。与此同时,我加了一个赏金。我不应该宽恕这种行为,但看看你是否能找到一些方法来解决这个问题或引起人们的注意。我想知道答案。
标签: c pointers structure language-features c11