Connecting Tech Pros Worldwide Help | Site Map

Building a struct within a struct correctly

bernd
Guest
 
Posts: n/a
#1: Jun 27 '08
Hi folks,

the code below "runs" with the following message:

Before structure assignment
Bus Error(coredump)

What I am trying to do is to define an "outer" structure containing an
inner one and then pass the address of the inner structure to the
pointer within the outer structure pointing to it's inner structure
(so that I can read in data into the outer structure and being able to
read it immediately from the inner structure). But obviously I am
doing something wrong.

Cheers


Bernd

#include <stdio.h>

main () {

int debug = 1 ;

typedef struct inner {

long var1 ;
char text[5] ;

} instruct ;


typedef struct outer {

long count ;
struct instruct *toinstruct ;

} outstruct ;


instruct *in ;
int in_size ;
in_size = sizeof( *in ) ;

in = malloc( in_size ) ;

outstruct *out ;
if ( debug == 1 ) { ( void ) fprintf(stderr, "Before structure
assignment\n" ) ; }

out->toinstruct = in ;

if ( debug == 1 ) { ( void ) fprintf(stderr, "After structure
assignment\n" ) ; }

int out_size ;
out_size = sizeof( *out ) ;
out = malloc( out_size ) ;

if ( debug == 1 ) { ( void ) fprintf(stderr, "Sizes: inner: %d -
outer: %d\n", in_size, out_size ) ; }

}
Fred
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Building a struct within a struct correctly


On Jun 25, 8:20*am, bernd <bew...@gmx.netwrote:
Quote:
Hi folks,
>
the code below "runs" with the following message:
>
Before structure assignment
Bus Error(coredump)
>
What I am trying to do is to define an "outer" structure containing an
inner one and then pass the address of the inner structure to the
pointer within the outer structure pointing to it's inner structure
(so that I can read in data into the outer structure and being able to
read it immediately from the inner structure). But obviously I am
doing something wrong.
>
Cheers
>
Bernd
>
#include <stdio.h>
>
main () {
>
* int debug = 1 ;
>
* *typedef struct inner {
>
* * *long var1 ;
* * *char text[5] ;
>
* *} instruct ;
>
* *typedef struct outer {
>
* * *long count ;
* * *struct instruct *toinstruct ;
'instruct' is a typedef - no need fot the struct keyword here.
This should be
instruct *toinstruct;

Also, better to place the typdefs outside the main function.
Quote:
>
* *} outstruct ;
>
* *instruct *in ;
* *int in_size ;
* *in_size = sizeof( *in ) ;
>
* *in = malloc( in_size ) ;
>
* *outstruct *out ;
You have declared 'out' to be a pointer to a outstruct.
You have not pointed it to anything yet.
Quote:
* *if ( debug == 1 ) { ( void ) fprintf(stderr, "Before structure
assignment\n" ) ; }
>
* *out->toinstruct = in ;
Now you try to dereference the uninitialized 'out' pointer.
Crash!
--
Fred Kleinschmidt
Ben Bacarisse
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Building a struct within a struct correctly


bernd <bew_ba@gmx.netwrites:
Quote:
What I am trying to do is to define an "outer" structure containing an
inner one and then...
This confused me at first. The one structure just contains a pointer
to the other. One structure contained in another would look
different.
Quote:
pass the address of the inner structure to the
pointer within the outer structure pointing to it's inner structure
(so that I can read in data into the outer structure and being able to
read it immediately from the inner structure).
I can't understand this. I can tell what is wrong with your code, but
the description of what you want is confusing.
Quote:
But obviously I am
doing something wrong.
Quote:
main () {
>
int debug = 1 ;
>
typedef struct inner {
long var1 ;
char text[5] ;
} instruct ;
>
typedef struct outer {
long count ;
struct instruct *toinstruct ;
} outstruct ;
>
instruct *in ;
int in_size ;
in_size = sizeof( *in ) ;
>
in = malloc( in_size ) ;
It is a good idea to test the result. It is unlikely to matter here,
but it is a good habit to get into in C.
Quote:
outstruct *out ;
if ( debug == 1 ) { ( void ) fprintf(stderr, "Before structure
assignment\n" ) ; }
>
out->toinstruct = in ;
Bang. At this point you can't use 'out' at all. It is an
uninitialised variable. You must set it to something before you do
anything with it. Since it is a pointer to a struct, you should set
it to point to one. This you go on to do...
Quote:
if ( debug == 1 ) { ( void ) fprintf(stderr, "After structure
assignment\n" ) ; }
>
int out_size ;
out_size = sizeof( *out ) ;
out = malloc( out_size ) ;
Now, provided the malloc worked (out != NULL) you could do the

out->toinstruct = in ;

but not earlier.
Quote:
if ( debug == 1 ) { ( void ) fprintf(stderr, "Sizes: inner: %d -
outer: %d\n", in_size, out_size ) ; }
>
}
--
Ben.
Tarique
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Building a struct within a struct correctly


bernd wrote:
Quote:
Hi folks,
>
the code below "runs" with the following message:
>
Before structure assignment
Bus Error(coredump)
>
What I am trying to do is to define an "outer" structure containing an
inner one
Ok

and then pass the address of the inner structure to the
Quote:
pointer within the outer structure pointing to it's inner structure
(so that I can read in data into the outer structure and being able to
read it immediately from the inner structure). But obviously I am
doing something wrong.
This is Quite confusing !
Quote:
>
Cheers
>
>
Bernd
>
#include <stdio.h>
>
main () {
>
int debug = 1 ;
>
typedef struct inner {
>
long var1 ;
char text[5] ;
>
} instruct ;
>
>
typedef struct outer {
>
long count ;
struct instruct *toinstruct ;
>
} outstruct ;
>
>
instruct *in ;
int in_size ;
in_size = sizeof( *in ) ;
>
in = malloc( in_size ) ;
>
outstruct *out ;
if ( debug == 1 ) { ( void ) fprintf(stderr, "Before structure
assignment\n" ) ; }
>
out->toinstruct = in ;
>
if ( debug == 1 ) { ( void ) fprintf(stderr, "After structure
assignment\n" ) ; }
>
int out_size ;
out_size = sizeof( *out ) ;
out = malloc( out_size ) ;
>
if ( debug == 1 ) { ( void ) fprintf(stderr, "Sizes: inner: %d -
outer: %d\n", in_size, out_size ) ; }
>
}
From - Wed

struct stackelement{

int etype;/*etype equals INT,FLOAT,STRING*/
/*depending on the type of the */
/*corresponding element */
union{
int iVal;
float fVal;
char pVal[ARRAYSIZE];
}element;
};

struct stack{
int top;
struct stackelement item[STACKSIZE]; /*Array Of Structures */
};


void pop( struct stack *p, struct stackelement *se )
{
if( p->top == -1 )
puts( "Underflow\n" );
else
{
if( se->etype == INT )
printf( "Deleted Integer is %d\n", p->item[(p->top)--].element.iVal );

else if( se->etype == FLOAT )
printf( "Deleted Float is %f\n" , p->item[(p->top)--].element.fVal );

else if( se->etype == STRING )
printf( "Deleted String is %s\n" , p->item[(p->top)--].element.pVal );

else
puts("Unknown Error");
}
return;
}



You are probably trying to do something similar.Its an unrelated code
segment but hope you can get an idea.
Closed Thread


Similar C / C++ bytes