1

when creating header file sometimes I need to have access to other struct that I have declared in the other header files. Currently I organize it by having the #include Directive as a tree so that if I need to call the struct in b from a I place a header underneath b in main. Probably a bad practice I am NEW to C.. don't be harsh :P where should I declared the struct for it to be accessible on the entire code?

should I create a header file with all my struct inside and then call it by doing an #include Directive on all the header files? what is the best practice to declare a struct for a beginner.

I currently access to other struct declared in other header files as such

//main file


#include "b.h"
#include "a.h" <--- I had to put a.h underneath b.h to access to the struct in b.h named test
#include "c.h" 

Osanda Gamage
  • 446
  • 2
  • 6
  • 16
dn70a
  • 73
  • 2
  • 8
  • In your question, you are talking about "constructors", but your question is tagged C, not C++. I wrote an answer on the assumption that you are talking about C++ constructors, but now I am not sure if you may have meant something else. If your question is about C++, then please tag it with the "c++" tag instead of the "c" tag. Otherwise, please elaborate on what you mean with "constructor", because C does not have C++ constructors. – Andreas Wenzel Jan 28 '22 at 04:10
  • oh yes i am in c – dn70a Jan 28 '22 at 04:12
  • i should then edit my question and change constructor into struct only.. but your answer could apply to c also .. gona try it out – dn70a Jan 28 '22 at 04:14
  • Yes, your question is better now, because you are no longer using the word constructor. I have rewritten my answer from C++ to C now. – Andreas Wenzel Jan 28 '22 at 04:31

1 Answers1

1

If the header file of struct A needs to know about struct B, then the header file which defines struct A should itself contain an #include directive that includes the header file of struct B, if it is defined in a different file.

It should not be necessary to fiddle around with the order of the #include directives in your main source file.

As long as all header files have proper header guards that protect against the same header file being included twice, then this should not be a problem.

A header guard of the file mystruct.h may look like this:

#ifndef MYSTRUCT_H_INCLUDED
#define MYSTRUCT_H_INCLUDED

//the main content of the header file goes here

#endif

This will set a preprocessor macro when the header file is included for the first time, and when the file is included a second time, the preprocessor will see that this macro has already been set, and will ignore the rest of the file.

On many compilers, it is sufficient to simply write

#pragma once

into the header file as a header guard, instead of using the three lines mentioned further above. However, this may not work in all compilers. See the following C++ question (which should also apply to C) for further information:

#pragma once vs include guards?

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • what do you think about creating a header file ´constructors.h´ and putting all the constructor in there? will that result to be quite messy? – dn70a Jan 28 '22 at 04:12
  • @dn70a: I don't understand what you mean with the word "constructor" in the context of C. I know what a C++ constructor is, but not what a C constructor is. – Andreas Wenzel Jan 28 '22 at 04:32
  • 1
    @dn70a: If you mean `struct` instead of "constructor", then yes, you can put all your `struct` definitions in your own header file. You can even put all your code into one single C file, and not create any header files at all, if you want. However, depending on the length of your program, this can get messy very quickly. Generally, the larger your code is, the better it is to divide it into several files. – Andreas Wenzel Jan 28 '22 at 04:35