I have 3 files
//--------------reg_des.h---------------
struct reg
{
unsigned int i : 4;
unsigned int j : 4;
};
extern struct reg;
//--------------reg_des.c---------------
struct reg xyz_reg = {.i = 2,.j = 1};
//--------------main.c---------------
#include "reg_des.c"
void display_onmodule(struct reg xyz_reg_1)
int main()
{
display_onmodule(xyz_reg);
}
void display_onmodule(struct reg xyz_reg_1)
{
.....
}
Here I have declared a struct in a header file and initialize variable of struct type in another source file. Actually I want to know is it right way to declare a struct which may be used by multiple source files?
extern struct reg;
means nothing. To define a structure you need just the definition, as you made in the header, To declare an instance of such a structure you need the declaration, i.e. including the header. To access the instantiationxyz_reg
of structurereg
you must define it asextern struct reg xyz_reg;
in the header file. – Frankie_Cxyz_reg
of structurereg
in different units (files) you must define it asextern struct reg xyz_reg;
in the header file, and include the header in each source. – Frankie_Cextern
reference in an header file? I made no mention, to be honest I haven't even notice it, that including C files in other C files is a good or bad idea. – Frankie_Cconst
qualified ones. – Lundin