We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I am using a structure named my_struct which is to be declared as extern. struct my_struct{ int a; int b; }; struct my_struct first; struct my_struct second; How do I declare the structure in the main file and use the structures first and second as extern in the other c files of the same project.
Don't forget those <pre> and </pre> tags! all your files need to be able to "see" the structure type definition; only one file must actually define the variables (as it is the definition which allocates storage):
//main.h // Define the structure so that all files can "see" it struct my_struct { int a; int b; }; // Declare the externs extern struct my_struct first; extern struct my_struct second;
//main.c // The header is needed for the struct definition; // It doesn't hurt to include the externs, // and ensures that declarations & definitions match! #include main.h // Define the structures - this allocates storage struct my_struct first; struct my_struct second; etc
//other.c // Include struct definition & extern declarations #include main.h etc