gpt4 book ai didi

header 中结构的 C 前向声明

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

我试图在函数中传递 struct 指针。我在 file1.h 中有一个 typedef,并且只想将该 header 包含到 file2.c,因为 file2.h 只需要指针。在 C++ 中,我会像我在这里所做的那样编写,但使用 C99 是行不通的。如果有人对如何在没有完整定义的情况下传递 struct 指针有任何建议,我们将不胜感激。编译器-gcc。

文件1.h

typedef struct
{
...
} NEW_STRUCT;

文件2.h

struct NEW_STRUCT;

void foo(NEW_STRUCT *new_struct); //error: unknown type name 'NEW_STRUCT'

文件2.c

#include "file2.h"

#include "file1.h"

void foo(NEW_STRUCT *new_struct)
{
...
}

最佳答案

我认为您只需命名您的结构,并对其进行前向声明,然后重新对其进行类型定义。

第一个文件:

 typedef struct structName {} t_structName;

第二个文件:

  struct stuctName;
typedef struct structName t_structName

关于 header 中结构的 C 前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17913871/

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