473,378 Members | 1,447 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,378 software developers and data experts.

Building a struct within a struct correctly

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 ) ; }

}
Jun 27 '08 #1
3 2001
On Jun 25, 8:20*am, bernd <bew...@gmx.netwrote:
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.
>
* *} 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.
* *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
Jun 27 '08 #2
bernd <be****@gmx.netwrites:
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.
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.
But obviously I am
doing something wrong.
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.
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...
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.
if ( debug == 1 ) { ( void ) fprintf(stderr, "Sizes: inner: %d -
outer: %d\n", in_size, out_size ) ; }

}
--
Ben.
Jun 27 '08 #3
bernd wrote:
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
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 !
>
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.
Jun 27 '08 #4

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

Similar topics

7
by: Capstar | last post by:
Hi NG, Does the following code invoke undefined behaviour? I compiled it and it runs fine. I think this is fine because I think the last 3 arguments of struct a are organised in memory the same...
19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
21
by: hermes_917 | last post by:
I want to use memcpy to copy the contents of one struct to another which is a superset of the original struct (the second struct has extra members at the end). I wrote a small program to test...
7
by: Urs Wigger | last post by:
In a C++ project, I have the following struct definition: struct GridModeDataT { double dVal1; double dVal2; double dVal3; long ...
0
by: chijazz | last post by:
I have the following unmanaged code: typedef struct sicCrdPgInfo { short bProg; short wAA1Limit; short bFree; short wPad3; BYTE ucPCB; } tsicCrdPgInfo;
5
by: Ufit | last post by:
Compiling the following code (C++): struct _Item { int var1; ....... _Item *pPointer;
6
by: Yi Xing | last post by:
Hi, I need to read specific lines of huge text files. Each time, I know exactly which line(s) I want to read. readlines() or readline() in a loop is just too slow. Since different lines have...
5
by: emmanuel.rivoire | last post by:
Hello, I spent almost a week to be able to embed Python within my C++ game engine. I wrote a mini-tutorial of what I was able to do so far here :...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.