以下是MWE:

#include <stdlib.h>

typedef struct node_type {
    struct node* next;
    struct node* prev;
} node;

int main(int argc, char* argv[]) {
    node* n = (node*)malloc(sizeof(node));
}

在 struct 体定义中,node*之前没有struct将出错; node尚未定义.但是,struct node*可以正常工作,尽管此时只定义了struct node_type.为什么会这样?

直观地说,如果 struct 的字段定义为struct node_type*,或者之前有显式的tyecif并且字段是node*类型,它就会进行编译.然而,我不明白为什么内联typlef允许字段是类型struct node*,而不是类型node*.

推荐答案

如果 struct 定义中的NODE*之前没有 struct ,则会出错; node 尚未定义.但是,struct node*工作得很好,甚至 尽管在这一点上只定义了struct node_type.

在此tyecif声明中

typedef struct node_type {
    struct node* next;
    struct node* prev;
} node;

声明了两种 struct 类型.第一个struct node_type是完整类型,第二个struct node是不完整类型.

除了这些声明之外,还为类型说明符struct node_type引入了别名node.

你可以写信给我

node* n = (node*)malloc(sizeof(node));

但你不能写

n->next = ( node * )malloc( sizeof( node ) );

n->next = malloc( sizeof( struct node ) );

因为类型struct node是不完整的类型.它的大小不得而知.和struct node *node *是不兼容的指针类型.

As f或 your question then if you will write f或 example

typedef struct node_type {
    node* next;
    node* prev;
} node;

那么在这些成员声明中,

node* next;
node* prev;

the name node is not defined yet. So the compiler issues an err或 message.

C++相关问答推荐

char为16位且Short也为16位的c环境合法吗

问关于C中的指针和数组

当包含头文件时,gcc会发出隐式函数声明警告

VS代码C/C++扩展intellisense无法检测环境特定函数'

如何将不同长度的位转换成字节数组?

我编译了一个新的c程序,并收到以下错误

当输入负数时,排序算法存在问题

函数内的局部字符指针

GTK3按钮信号错误

当b是无符号字符时,int a=(b<;<;2)>;>;2;和int a=b&;0x3F;之间有什么区别?

覆盖读取函数,但当文件描述符为3或4时,我有问题

C11/C17标准允许编译器清除复合文字内存吗?

初始成员、公共初始序列、匿名联合和严格别名如何在C中交互?

C-try 将整数和 struct 数组存储到二进制文件中

带有数组指针的 struct 在print_stack()函数中打印随机数

OMP并行嵌套循环

C 语言中 CORDIC 对数的问题

使用fread()函数读取txt文件

C 中从 Unix 纪元时间转换的损坏

在 C 中的 scanf() 格式说明符中使用宏获取字符串长度