Struct in header file | SoloLearn: Learn to code for FREE!


Struct in header file

Can i declare a struct in a header file test.h, define it in test.c file and after that use the struct in otherFile.c where i included the header file test.h. Or do i have to define the struct in the header file completly to use it in .c files where i included the header?


11/12/2019 9:32:23 PM


12 Answers

New Answer


You can do it both ways but second way is more modular and gives more flexibility. With first way you would need to include test.c too everywhere you want to use your struct.


You just need to include "test.h" header file in otherFile.c then compile otherFile.c and test.c together from command line gcc test.c otherFile.c or if you are using an IDE you need to add test.c to the same project where you have otherFile.c and compile


The other way is to have the complete definition of your struct and functions in the header fiile. Structure definition in the header is fine but function definitions in header is generally not recommended (but it totally depends on your project design)


To use a function completely defined in .c file into another .c file you need to do something like this //test.c int fun() { return 10; } // otherFile.c #include <stdio.h> extern int fun(); // <<== this line is important int main() { printf("%d", fun()); } Then compile both files together Not sure if it can be done with structure.


When writing a header file always use the include guard, it will ensure that the header file is included only once classic way and the most portable way to do it // mystruct.h #ifndef _MYSTRUCT_H_ (can be all lowercase or mix) #define _MYSTRUCT_H_ typedef struct mystruct { int x; char y; } mystruct; #endif other ways to do it // mystruct.h #pragma once (may not be supported by all compilers) your struct definition


I have done that: In header file test.h( surrounded by #ifndef #define .... #endif): typedef struct mystruct mystruct; void mystruct func(); And in test.c file: struct { int x; int y}; void mystruct func(){ ..... definition } How would i use this function and struct in otherFile.c? (And btw thank you very much for your help :D)


I also have funktion declarations in the header file which i define in test.c. When i also include test.c the Compiler tells me that i have multiple definitons of the functions Is it even possible to declare the functions and the struct in the same test.c file and use the functions and the struct in otherFile.c ?


And of course i included test.h in test.c


Thank you thats actually my problem.. The complier tells me "error: dereferencing pointer to incomplete type 'mystruct'" (I thought thats not the problem so i used easier examples) My files actually look like this: test.h : typedef struct struct1 struct1; typedef struct struct2 struct2; struct2* func(struct1* s); test.c: struct struct1 { struct2* x; }; struct struct2 { struct2* y; }; struct2* func(struct1* s){ return s->x; } Otherfile.c #include "test.h" int main(){ ... struct1* 'm' points to allocated memory..... m->x } This only works when i define the struct already in the header file. When i do it like this the compiler tells me that im dereferencing pointer to incomplete type struct1 with "m->x"


Thank you that was really helpful 😊


It always works when i define the struct in the header and only the function in test.c But when i define everything in c the otherFile.c it does not So i thought its maybe not possible that way (But could also be another problem then)