Using a C structure in multiple files.

preview_player
Показать описание
If your struct is only going to be used by a single C program, including it in the .c file works fine. If multiple C programs will be using the structure, you can place the structure in a header file and re-use it in multiple .c programs.
Рекомендации по теме
Комментарии
Автор

Thanks for this. I followed a cpp tutorial that briefly talked about creating headers for functions, where you define the function in its own file, then declare the function in your header file. I thought you had to do the same thing for structs and I wasted like 2 hours trying to debug my code and understand why my project wouldn't compile before I came here and realized that the struct isn't supposed to be outside of the header xD

theclingyfox
Автор

ty, i cant understand my teacher and u helped me in this 2 days deadline.

SlenderKS
Автор

I really want to know how do I use include more than one of my headers, when I try to do this I got errors because no one of them recognize each other.

alvarohigino
Автор

If I have my header file called function.h with the declaration of a function there and a file called function.c with the file definition in it. If I want to call that function from main.c I will put #include "function.h" in main.c then compile without linking function.c and main.c than link the two object files together and it'll work? And what if the function in function.c has as a parameter a array of structure defined in main.c, then function.c won't compile, do I have to copy and paste the structure definition in function.c too?

zdaavi