473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

nested structures and initialization

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;
}
sptr->TR->operationalfile=NULL;
sptr->TR->tunedfile=NULL;
sptr->TR->radarfile=NULL;
sptr->TR->amsu_flag={0.0};
sptr->TR->radar={0.0};
sptr->TR->pcpn1={0.0};
sptr->TR->pcpn2={0.0};
sptr->TR->pcpn3={0.0};

sptr->Sp->N18->table_10={0.0};
sptr->Sp->N18->table_20={0.0};
sptr->Sp->N18->table_30={0.0};
sptr->Sp->N18->table_40={0.0};
sptr->Sp->N18->table_50={0.0};
sptr->Sp->N18->table_60={0.0};
sptr->Sp->N18->procent_amsu = 0.0;

sptr->Sp->N17->table_10={0.0};
sptr->Sp->N17->table_20={0.0};
sptr->Sp->N17->table_30={0.0};
sptr->Sp->N17->table_40={0.0};
sptr->Sp->N17->table_50={0.0};
sptr->Sp->N17->table_60={0.0};
sptr->Sp->N17->procent_amsu = 0.0;
.....

}

The errors begin with these:

error: incompatible types in assignment
error: incompatible types in assignment
error: incompatible types in assignment
error: parse error before '{' token
error: parse error before '{' token

and keeps on going.

Could some please render some help in doing this correctly?

Any help is truly appreciated.
/S

Oct 31 '07 #1
8 5877
On Wed, 31 Oct 2007 09:18:29 -0000, Sheldon <sh******@gmail.com>
wrote:
>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:
<snip>
>typedef struct super Super;
<snip>
>struct super {
Transient *TR;
Satellites *Sp;
Satellites *Su;
Satellites *Au;
Satellites *Wi;
};
<snip>
>Super *InitStruct(void) {

Super *sptr=NULL;

if(!(sptr=malloc(sizeof(Super)))) {
return NULL;
}
<snip remainder>

Well the first thing that leaps out is that you've allocated space for
a "struct super", which is simply five pointers. Nowhere do you
allocate the memory that those pointers point at. I'm not even going
to try and see what else is wrong :-)
--
PGP key ID 0xEB7180EC
Oct 31 '07 #2
On 31 Okt, 10:44, Keith Willis <m...@privacy.netwrote:
On Wed, 31 Oct 2007 09:18:29 -0000, Sheldon <shejo...@gmail.com>
wrote:
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:

<snip>
typedef struct super Super;

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

<snip>
Super *InitStruct(void) {
Super *sptr=NULL;
if(!(sptr=malloc(sizeof(Super)))) {
return NULL;
}

<snip remainder>

Well the first thing that leaps out is that you've allocated space for
a "struct super", which is simply five pointers. Nowhere do you
allocate the memory that those pointers point at. I'm not even going
to try and see what else is wrong :-)
--
PGP key ID 0xEB7180EC
Ok, fair enough. How would you do it then? Let's say a 3 tier struct?
Can you give an example that I can expland on?

/S

Oct 31 '07 #3
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;
};
Why do you define 6 separate on-dimensional tables, rather than a single
table with a leading dimension of 6? I suspect it will simplify the code
that uses this structure.

I would also recommend combining those typedefs with the corresponding
struct declarations:

typedef struct data {....} Data;

I find that easier to understand; YMMV.
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];
};
Same comment about the pcpn family.
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) {
That's a bad naming choice. InitSuper would be better. In general, you
might have quite a lot of different structures to initialize. In this
particular case, you have two of them, which is enough to justify not
naming either function "InitStruct".
Super *sptr=NULL;

if(!(sptr=malloc(sizeof(Super)))) {
return NULL;
}
At this point, none of the pointer members of *sptr have been
initialized. Any use of the value of those pointers has undefined
behavior. You need to allocate memory for each of the pointed at
objects. Note: with this data structure you're going to have to make
LOTS of separate memory allocations. If any one of those fails, then all
of the allocations that succeeded should be free()d. This can get very
complicated very fast. I avoid building structures like this, for
precisely that reason. Therefore, the solution I use below is not the
result of lots of experience with this kind of thing, but it's a lot
better than other techniques I've seen used:

bool successful = true;
// Initialize to make sure that we can free() them without worrying
// about whether they have been allocated:
sptr->TR = NULL;
sptr->Sp = NULL;
// Similarly for other pointer members
if(!(sptr->TR = malloc(sizeof Transient)) ||
!(sptr->Sp = malloc(sizeof Satellites)) ||
// Similarly for other pointer members
){
successful = false;
}

....
sptr->TR->amsu_flag={0.0};
The curly brackets are a syntax error in this context. I think you are
thinking of initialization syntax, but that doesn't apply to dynamically
allocated memory. You'll have to write a loop to initialize that array.
You'll be doing a lot of this, so I'd recommend creating a subroutine
analogous to memset() which works on floats rather than unsigned char.

If you use calloc() instead of malloc(), or use memset() after malloc(),
on many implementations the floats will be properly initialized. That's
because floating point formats which use a representation for 0.0 that
has all of its bits set to 0 are fairly common. However, that's not
guaranteed by the C standard, so you should not use that approach in
code that needs to be portable.

At this point, none of the pointer members of sptr->Sp have been
initialized. Any attempt to use their current value has undefined
behavior. You need to allocate memory for each one, just as I described
above for the pointer members of sptr itself. You'll need to do this
many times, so I recommend writing as subroutine to handle it. Later on,
you'll need to deallocate all of that memory; again, you should use a
subroutine. I'll need to refer to those routines down below, so I'll
call them InitSatellite() and FreeSatellite(). Note: if the argument
passed to FreeSatellite is null, it should return without doing
anything, the same way that free() itself does.

Remember to set successful = false if any of the InitSatellite() calls
fails.

This code should (with appropriate re-writes) be part of InitSatellite():

....
sptr->Sp->N18->table_10={0.0};
....

if(!successful)
{ // Free everything, regardless of whether it's been allocated.
free(sptr->TR);
FreeSatellite(sptr->Sp);
free(sptr->Sp);
// Similarly for other Satellites* members.
free(sptr);
return NULL;
}

return sptr;
// Your original did not appear to include a return statement?
}
Oct 31 '07 #4
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?)
Oct 31 '07 #5
Mark Bluemel wrote:
....
This should work though (I think) :-

sptr->TR->amsu_flag=0;
amsu_flag is an array of floats. Therefore it's not a modifiable lvalue
(6.3.2.1p1), so it can't be the left operand of an assignment operator
(6.5.16p2).
Oct 31 '07 #6
James Kuyper wrote:
Mark Bluemel wrote:
...
>This should work though (I think) :-

sptr->TR->amsu_flag=0;

amsu_flag is an array of floats. Therefore it's not a modifiable lvalue
(6.3.2.1p1), so it can't be the left operand of an assignment operator
(6.5.16p2).
I did wonder, but it compiled (gcc) even with strict options...
Oct 31 '07 #7
On 31 Okt, 12:44, Mark Bluemel <mark_blue...@pobox.comwrote:
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


Oct 31 '07 #8
On Wed, 31 Oct 2007 13:11:09 -0000, Sheldon <sh******@gmail.com>
wrote:
<much snippage>
>
retv->operationalfile = NULL;
retv->tunedfile = NULL;
retv->radarfile = NULL;
<more snippage>
>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
I told you in the email I sent you that your initialisation of these
strings was nonsense. You are trying to assign the value NULL to a
variable of type array-of-char. You need to set the first character
of the array-of-char to the value '\0', thus:

retv->operationalfile[0] = '\0';

Alternatively, initialise the entire string to zeros (overkill):

memset(retv->operationalfile, 0, sizeof retv->operationalfile);
>What I am missing here?
Clues?
--
PGP key ID 0xEB7180EC
Oct 31 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ori Y | last post by:
Hi , i'm having some difficulties regarding using the Python/c api. I'm trying to bulid a deeply nested datastructure (i.e dict within a dict or list within a dict) Building the data stracture...
3
by: Steve | last post by:
A coworker just returned to C++ coding after a long hiatus and asked me how to accomplish this task. What seemed liked simple issue, we have been unable to resolve. He has declared a structure that...
11
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
0
by: Andre | last post by:
Hi, I have a problem with marshaling nested structures struct a { struct b s_b; struct d s_d;
1
by: Olav Tollefsen | last post by:
I have a somewhat complex data structure to display on a web page: Item A - Sub Item 1: a, b, c, ... - Sub Item 2: a, b, c, ... - ... Item B - Sub Item 1: a, b, c, ... - Sub Item 2: a, b, c,...
6
by: Computer Wizard | last post by:
Hello, I am really scrwed up with the following stuff: struct employee { char* employee_name; }; struct department {
2
by: samba | last post by:
I having a nested structures which are referenced with pointer variables #include <stdio.h> struct test { int i; char *mytest; }; struct sample {
3
by: Amit_Basnak | last post by:
Dear Friends I have the follwoing function "tss_fe_get_own_info" which has the arguments as shows below tss_fe_get_own_info(char *user_id, tss_user_profile_t **p_buf_UserSecInfo, error_status_t...
25
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.