我对 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;
}
这个答案表明我可能在询问未命名的结构而不是匿名结构。我完全误解了匿名结构的性质吗?