I am trying to pass struct
pointer in function. I have a typedef
in file1.h, and want to only include that header to file2.c, because file2.h only need pointer. In C++ I would just write like I did here, but using C99 it doesn't work. If someone has any suggestions how to pass struct
pointer without full definition it would be very appreciated. Compiler - gcc.
我试图在函数中传递struct指针。我在file1中有一个类型定义。h,并只想包含文件2的头。c,因为file2。h只需要指针。在c++中,我可以像这里那样写,但是用C99不行。如果有人对如何在没有完整定义的情况下传递struct指针有任何建议,我们将非常感激。编译器gcc。
file1.h
file1.h
typedef struct
{
...
} NEW_STRUCT;
file2.h
file2.h
struct NEW_STRUCT;
void foo(NEW_STRUCT *new_struct); //error: unknown type name 'NEW_STRUCT'
file2.c
file2.c
#include "file2.h"
#include "file1.h"
void foo(NEW_STRUCT *new_struct)
{
...
}
2 个解决方案
#1
9
I think you just have to name your structure, and do a forward declaration of it and after re typedef it.
我认为你只需要给你的结构命名,然后对它做一个前向声明,然后在重新定义它之后。
First file:
第一个文件:
typedef struct structName {} t_structName;
Second file:
第二个文件:
struct stuctName;
typedef struct structName t_structName
#2
0
You may try this:
你可以试试这个:
file1.h
file1.h
typedef struct _NEW_STRUCT // changed!
{
...
} NEW_STRUCT;
file2.h
file2.h
struct _NEW_STRUCT; // changed!
void foo(struct _NEW_STRUCT *new_struct); // changed!
file2.c
file2.c
#include "file2.h"
#include "file1.h"
void foo(NEW_STRUCT *new_struct)
{
...
}
#1
9
I think you just have to name your structure, and do a forward declaration of it and after re typedef it.
我认为你只需要给你的结构命名,然后对它做一个前向声明,然后在重新定义它之后。
First file:
第一个文件:
typedef struct structName {} t_structName;
Second file:
第二个文件:
struct stuctName;
typedef struct structName t_structName
#2
0
You may try this:
你可以试试这个:
file1.h
file1.h
typedef struct _NEW_STRUCT // changed!
{
...
} NEW_STRUCT;
file2.h
file2.h
struct _NEW_STRUCT; // changed!
void foo(struct _NEW_STRUCT *new_struct); // changed!
file2.c
file2.c
#include "file2.h"
#include "file1.h"
void foo(NEW_STRUCT *new_struct)
{
...
}