Typedef stuct with forward declaration in C

I have something like:

typedef struct Data DATA, *DATA_PTR;
typedef struct Units UNITS, *UNITS_PTR;

struct Data
{
    double miscData;
    UNITS units;
};

struct Units
{
    double x[2];
    double y[2];
    double z[2];
};

in my project_typedef.hfile.

In another file, I have something like:

void fileInput(DATA_PTR data)
{
     //usual declarations and other things
     data->miscData = 0; //Works!
     data->units.x[0] = 5; //Doesn't work
     //etc...
}

However, this does not work, as units are declared after the data in project_typedef.h(if I switch the order in which it works). The error I get “to the left of .x 'should be of type struct / union.” I thought that the following declaration would be accepted in this declaration. Why not?

+5
source share
4 answers

Data, . UNITS , . ( UNITS_PTR , .)

UNITS Data, .

( @cnicutar , x .)

+10

- , . , , .

+5

struct. , . struct, , , .

+1

... UNITS units struct Data, units struct Units struct Data... .. units Data definition.. .

and with regard to direct declaration, this does not work since whenever the structural variable is defined, the compiler first allocates the memory needed for structural ( structure elements that do not have allocated memory if they are not related to the structure of the variable type . That's why structure variables cannot be initialized inside the structure template) .. :)

0
source

All Articles