file1.h: Library code. CANT change it
#ifndef FILE1_H_ #define FILE1_H_ typedef struct { int x; } MY_STRUCT; #endif /* FILE1_H_ */
file2.h: user defined. can be changed. How to forward declare above typedef struct which has no tag ?
#ifndef FILE2_H_ #define FILE2_H_ struct MY_STRUCT; void print(struct MY_STRUCT * obj); #endif /* FILE2_H_ *
file2.c: user defined. can be changed
#include "file2.h" #include "file1.h" #include <stdio.h> void print(struct MY_STRUCT * obj) { printf("x: %d", obj->x); }
main.c
#include "file2.h" #include "file1.h" int main(void){ MY_STRUCT obj1; obj1.x = 100; print(&obj1); }
The code can be seen here. https://paiza.io/projects/wa2PCvUswWyyAzdxxjggxQ?language=c
It's not possible to "forward-declare" a typedef . You will have to give the struct a tag, e.g.:
typedef struct my_struct MY_STRUCT; // ... later struct my_struct { int x; };
1.4m articles
1.4m replys
5 comments
57.0k users