Reputation: 89
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.
file1.h
typedef struct
{
...
} NEW_STRUCT;
file2.h
struct NEW_STRUCT;
void foo(NEW_STRUCT *new_struct); //error: unknown type name 'NEW_STRUCT'
file2.c
#include "file2.h"
#include "file1.h"
void foo(NEW_STRUCT *new_struct)
{
...
}
Upvotes: 6
Views: 17418
Reputation: 160
You may try this:
file1.h
typedef struct _NEW_STRUCT // changed!
{
...
} NEW_STRUCT;
file2.h
struct _NEW_STRUCT; // changed!
void foo(struct _NEW_STRUCT *new_struct); // changed!
file2.c
#include "file2.h"
#include "file1.h"
void foo(NEW_STRUCT *new_struct)
{
...
}
Upvotes: 2
Reputation: 433
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
Upvotes: 10