gpt4 book ai didi

c - 为什么 "struct T* next"在 T 不是现有类型时编译?

转载 作者:太空狗 更新时间:2023-10-29 15:20:05 24 4
gpt4 key购买 nike

我在 Windows 上使用 MinGW。我正在构建链表,但我对此感到困惑。

#include <stdio.h>
#include <stdlib.h>

typedef struct Data
{
int x;
int y;
struct BlaBla * next; /*compiles with no problem*/
}List;

int main(void)
{
List item;
List * head;
head = NULL;
return 0;
}

我现在认为结构不能有结构变量(对象,该结构的实例),但可以有该结构类型的指针。不知道指针可以是不存在类型的指针。 struct BlaBla * next;(不是链表,必须是struct Data * next,一般来说)

最佳答案

是的,你可以,因为编译器在第一次遇到未知类型名称时,会假设有一个somehwere 结构类型定义具有此名称。然后它将为您转发声明结构名称,让您将它用作指针,但您不能取消引用它,也不能对其进行指针运算(因为它是一个不完整的类型)。

关于c - 为什么 "struct T* next"在 T 不是现有类型时编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15515966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com