gpt4 book ai didi

c - c中的自引用结构

转载 作者:太空狗 更新时间:2023-10-29 15:13:48 50 4
gpt4 key购买 nike

我正在尝试将一些在 VS2010 中像 C++ 一样编译良好的代码移动到 c (gcc c99),但我遇到了编译错误。它与其他自引用结构问题略有不同,因为我有 2 个用户定义的类型,每个类型都包含指向彼此的指针。看来我的前向声明(forward declaration)还不够。

struct potato; //forward declare both types
struct tomato;

struct potato
{
potato* pPotato; //error: unknown type name ‘potato’
tomato* pTomato;

};

struct tomato
{
potato* pPotato;
tomato* pTomato;
};

为什么这在 gcc 99 中不起作用?为什么它可以作为 C++ 代码?我应该如何修改它以获得与 c99 相同的行为?

最佳答案

或者,typedef 它们都是

typedef struct potato potato; //forward declare both types
typedef struct tomato tomato;

struct potato
{
potato* pPotato;
tomato* pTomato;

};

struct tomato
{
potato* pPotato;
tomato* pTomato;
};

关于c - c中的自引用结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40942326/

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