Re: nested structures and initialization
- From: Mark Bluemel <mark_bluemel@xxxxxxxxx>
- Date: Wed, 31 Oct 2007 11:44:21 +0000
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?)
.
- Follow-Ups:
- Re: nested structures and initialization
- From: Sheldon
- Re: nested structures and initialization
- From: James Kuyper
- Re: nested structures and initialization
- References:
- nested structures and initialization
- From: Sheldon
- nested structures and initialization
- Prev by Date: Re: nested structures and initialization
- Next by Date: Re: nested structures and initialization
- Previous by thread: Re: nested structures and initialization
- Next by thread: Re: nested structures and initialization
- Index(es):
Relevant Pages
|