Re: nested structures and initialization



Sheldon wrote:
Hi,

Can anyone help with this problem with setting up nested structures
and initializing them for use.
I have created several structs and placed them in a super struct that
I will then pass to some functions. I have defined them in the
following manner:

typedef struct trans Transient;
typedef struct sats Satellites;
typedef struct data Data;
typedef struct super Super;

struct data {
float table_10[ROW*COL];
float table_20[ROW*COL];
float table_30[ROW*COL];
float table_40[ROW*COL];
float table_50[ROW*COL];
float table_60[ROW*COL];
float procent_amsu;
};

struct trans {
char operationalfile[MAXSTR_LENGTH];
char tunedfile[MAXSTR_LENGTH];
char radarfile[MAXSTR_LENGTH];
float amsu_flag[ARRAYSIZE];
float radar[ARRAYSIZE];
float pcpn1[ARRAYSIZE];
float pcpn2[ARRAYSIZE];
float pcpn3[ARRAYSIZE];
};

struct sats {
Data *pN18;
Data *pN17;
Data *pN16;
Data *pN15;
Data *pM02;
};

struct super {
Transient *TR;
Satellites *Sp;
Satellites *Su;
Satellites *Au;
Satellites *Wi;
};

Super* InitStruct(void);

Now when I try to intialize the structure I get a bunch of errors. The
InitStruct functions looks like this:

int main() {

Super *sptr;
/* initializing the super structure */
sptr = InitStruct();
if (sptr == NULL) {
fprintf(stderr,"Failed to initialize nested structure!\n");
exit(EXIT_FAILURE);
} else {
printf("Structure now initialized!\n");
}
return 1;
}

Super *InitStruct(void) {

Super *sptr=NULL;

if(!(sptr=malloc(sizeof(Super)))) {
return NULL;
}

if (!(sptr->TR = malloc(sizeof(Transient))) {
return NULL;
} /* and for the rest of the substructures */

sptr->TR->operationalfile=NULL;
sptr->TR->tunedfile=NULL;
sptr->TR->radarfile=NULL;
sptr->TR->amsu_flag={0.0};

You can't in general assign arrays - this syntax is allowed in initializers only, I think.

This should work though (I think) :-

sptr->TR->amsu_flag=0;

repeat as needed...

Personally I'd have been inclined to go encapsulated and have functions to return initialized substructures, I think. (Too much exposure to OO?)
.



Relevant Pages

  • Re: nested structures and initialization
    ... typedef struct sats Satellites; ... typedef struct super Super; ... float radar; ... fprintf(stderr,"Failed to initialize nested structure!\n"); ...
    (comp.lang.c)
  • nested structures and initialization
    ... typedef struct trans Transient; ... typedef struct sats Satellites; ... typedef struct super Super; ... float radar; ...
    (comp.lang.c)
  • Re: nested structures and initialization
    ... Satellites *Sp; ... Super *sptr=NULL; ... a "struct super", ...
    (comp.lang.c)
  • Re: Implementing OO
    ... struct myNewClass:myClass { ... of type myClass */ ... parameter or any struct who's super is of type myClass. ...
    (comp.compilers)
  • Re: strong/weak typing and pointers
    ... the sequence of bits, ... The struct type with so many bits being signs, exponent, significands, ... I have to have a float, to 'get the N-bit integer that gives the ... to know what struct type is most useful to interpret the ...
    (comp.lang.python)