Re: nested structures and initialization



On 31 Okt, 12:44, Mark Bluemel <mark_blue...@xxxxxxxxx> wrote:
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?)- Dölj citerad text -

- Visa citerad text -


Hi,

Thanks for your input. I took your advice and redesigned the script.
Now things are done in smaller steps. But I still get a persistent
error. Here are the new header and source files:
<snip>

typedef struct {
float thres_tables[THRESHOLDS][ROW*COL];
float procent_amsu;
} Data;

typedef struct {
char operationalfile[MAXSTR_LENGTH];
char tunedfile[MAXSTR_LENGTH];
char radarfile[MAXSTR_LENGTH];
float amsu_flag[ARRAYSIZE];
double variables[COMP_VAR][ARRAYSIZE];
Data* satid;
} SUPER;

SUPER* NewSatellite(void);

/* pointers for sub strings */
char *p;
int counter;
SUPER *retv=NULL;

/* initializing the super structure */
retv = NewSatellite();
if (retv == NULL) {
fprintf(stderr,"Failed to initialize nested structure!\n");
exit(EXIT_FAILURE);
} else {
printf("Structure now initialized!\n");
}
/* read the file list one at a time */
file = fopen(datafile,"r");
if (file == NULL) {
fprintf(stderr,"Error: can't open file %s\n", datafile);
exit(EXIT_FAILURE);
} else {
printf("File successfully opened. Reading line by line...\n");
j = 0;
while (fgets(line,MAXLINE_LENGTH,file) != NULL) {
printf("Line %d\t%s\n",++j,line);
/* cutting the strings using delimiter ' ' */
p = strtok(line," ");
counter = 0;
while (p != NULL) {
if (counter == 0) strcpy(retv->tunedfile,p);
p = strtok(NULL," ");
printf("Sub string: %s\n",retv->tunedfile);
counter++;
}
} /* end while */
printf("EOF reached. Closing the file...\n");
fclose(file);
} /* else section if file is open without problem */
return 1;
} /* end main */

/* Initialize the structure */
SUPER* NewSatellite(void) {

int i,j;
SUPER *retv = NULL;

if ((retv = malloc(sizeof(SUPER))) == NULL) {
fprintf(stderr,"Failed to allocate memory for structure!\n");
exit(EXIT_FAILURE);
}

retv->operationalfile = NULL;
retv->tunedfile = NULL;
retv->radarfile = NULL;
retv->satid->procent_amsu = 0.0;
for (i=0; i < COMP_VAR; i++) {
for (j = 0; j < ARRAYSIZE; j++) {
retv->variables[i][j] = 0.0;
retv->amsu_flag[j] = 0.0;
}
}
for (i=0; i < THRESHOLDS; i++) {
for (j = 0; j < ROW*COL; j++) {
retv->satid->thres_tables[i][j] = 0.0;
}
}
return retv;
};

but the error remains:
c: In function `main':
c:20: warning: unused variable `i'
c: In function `NewSatellite':
c:73: error: incompatible types in assignment
c:74: error: incompatible types in assignment
c:75: error: incompatible types in assignment
c:76: warning: statement with no effect


What I am missing here?

/S








.



Relevant Pages

  • Re: nested structures and initialization
    ... typedef struct trans Transient; ... float radar; ... you might have quite a lot of different structures to initialize. ... You need to allocate memory for each one, just as I described above for the pointer members of sptr itself. ...
    (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
    ... and initializing them for use. ... typedef struct trans Transient; ... typedef struct super Super; ... float radar; ...
    (comp.lang.c)
  • [2.6 patch] SCTP: possible cleanups
    ... typedef struct sctp_cmsgs { ... /* Initialize a block of memory as a command sequence. ... /* Get the size of a DATA chunk payload. ... +static void sctp_datamsg_init ...
    (Linux-Kernel)
  • Re: [BUG] 2.6.24-rc2-mm1 - kernel bug on nfs v4
    ... it does not "correctly" initialize the waitqueue. ... *parent, struct inode *dir, struct n ... +static inline void ...
    (Linux-Kernel)