first usage of headers

Hi, another question of mine:

Suppose I wanted to use a function in my main() that is written in another file.
I have read that you have to uses headers then. I also want to use structures in my main and, in fact, the function needs a structure as an argument.
So in the beginning, I have to include the function header and define my structure:

1
2
3
4
5
6
7
8
9
10
11
12
#include "my_function.h"

struct test_struct{
int x;
};

main(){
//some code
test_struct somestruct{1};
my_function(somestruct);
//some code
}


When I write my function definition in another file, i.e. the header, the compiler also seems to need the structure definition. So I would write the structure definition again at the beginning of my header.
But this seems to be inefficient. Do I really have to specify my structure in both files?

Regards!
when you #include a file is the same that copy-paste its content at that point
so you could #include "my_struct.h" where needed.
No, if the structure is defined in the header file you won't need to define the structure in the source file just #include the header file in the file that needs to structure definition.

By the way main() should be defined to return an int: int main().

Can you post all your code? Include the header file and the main file.
@jlb

thank you, this makes sense! So in my main() file, the compiler will also compile the included headers first and then understand that there is no problem, if I use a struct that has been declared in the header?

@Manga
I don't have the code yet, only played around with this header stuff and thought about this 'problem'.
Topic archived. No new replies allowed.