I have a doubt about declaration of external variables. I'm working in a project that is already working, and it is like below (it is an example based in the real program):
#include "mystruct.h"
extern long a ;
extern strc_1 s ;
extern STRC_2 *ss;
In my main program I have
#include "file1.h"
long a ;
strc_1 s ;
main()
{
a = 10;
s.variableX = 10;
s.variableY = 50;
ss.J = 50;
ss.J = 250;
}
So, my question is: Why does not the pointer 'ss' need to be declared in the main program? And why does it work? I mean, the variables 'a' and 'strc_1' were declared in the main program because I use them, however, I use 'ss' but I did not declared it in the main program.
long a ;
strc_1 s ;
These in main.c
are called definitions, not declarations. (To be precise, all definitions are also declarations).
The reason you don't have to define ss
is because it's defined in some other source file. As long as the declaration of ss
(which is in header1.h
) can be seen, it will compile fine.