Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C forward declaration of struct in header

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)
{
    ...
}
like image 379
t_smith Avatar asked Jul 28 '13 23:07

t_smith


1 Answers

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
like image 149
Joyas Avatar answered Oct 12 '22 17:10

Joyas