473,406 Members | 2,352 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,406 software developers and data experts.

storing related structs

I have two related structs:
struct A
{
int x ;
void * data ;
};

and

struct B
{
int x, y ;
double z ;
void * data1, *data2 ;
};
These structures (not pointers to them), need to be stored in a another
struct:

struct C
{
unsigned id ;
char name ;

DATA_TYPE the_struct ;
};
Where DATA_TYPE is EITHER a struct A or struct B.

For compatability with existing libraries, I cannot use a void * (for
instance), and cast between struct A and B.
Is there a soln to this problem ?
Jun 30 '07 #1
14 1341

"Gray Alien" <gr**@andromeda.comha scritto nel messaggio news:Z7******************************@bt.com...
>I have two related structs:
struct A
{
int x ;
void * data ;
};

and

struct B
{
int x, y ;
double z ;
void * data1, *data2 ;
};
These structures (not pointers to them), need to be stored in a another struct:

struct C
{
unsigned id ;
char name ;

DATA_TYPE the_struct ;
};
Where DATA_TYPE is EITHER a struct A or struct B.

For compatability with existing libraries, I cannot use a void * (for instance), and cast between struct A and B.
Is there a soln to this problem ?
struct C {
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;
union {
struct A str_A;
struct B str_B;
} the_struct;
};

A union is like a struct, but can only hold an element at a time.
You access members of unions like members of struct. For example,
in the case above to access the double of a struct B in a struct C,
you'll have to use mystructC.the_struct.str_B.z
Or try to find a better way to do what you're trying to do.
Jun 30 '07 #2
On 30 Jun, 13:15, "Army1987" <please....@for.itwrote:
"Gray Alien" <g...@andromeda.comha scritto nel messaggionews:Z7******************************@bt. com...
I have two related structs:
struct A
{
int x ;
void * data ;
};
and
struct B
{
int x, y ;
double z ;
void * data1, *data2 ;
};
These structures (not pointers to them), need to be stored in a another struct:
struct C
{
unsigned id ;
char name ;
DATA_TYPE the_struct ;
};
Where DATA_TYPE is EITHER a struct A or struct B.
For compatability with existing libraries, I cannot use a void * (for instance), and cast between struct A and B.
Is there a soln to this problem ?

struct C {
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;
union {
struct A str_A;
struct B str_B;
} the_struct;

};

A union is like a struct, but can only hold an element at a time.
You access members of unions like members of struct. For example,
in the case above to access the double of a struct B in a struct C,
you'll have to use mystructC.the_struct.str_B.z
Or try to find a better way to do what you're trying to do.
To the OP: One way of doing it would be to use a union, as Army1987
has suggested. However, if space isn't an issue, you might do better
including both a struct A and a struct B, and just using the one that
you want. This would seem to have less chance of going horribly wrong,
and might make your code clearer. Is there some reason why this would
not work?

Hope that helps.
Paul.

Jun 30 '07 #3
Gray Alien wrote, On 30/06/07 12:28:
I have two related structs:

struct A
<snip>
struct B
<snip>
These structures (not pointers to them), need to be stored in a another
struct:

struct C
{
unsigned id ;
char name ;
DATA_TYPE the_struct ;
};
Where DATA_TYPE is EITHER a struct A or struct B.

For compatability with existing libraries, I cannot use a void * (for
instance), and cast between struct A and B.

Is there a soln to this problem ?
Yes, use a union of the two struct types. Obviously you need something
to indicate what is stored so you know how to access it.
--
Flash Gordon
Jun 30 '07 #4


Army1987 wrote:
"Grey Alien" <gr**@andromeda.comha scritto nel messaggio news:Z7******************************@bt.com...
<snip>
</snip>
>>Is there a soln to this problem ?

struct C {
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;
union {
struct A str_A;
struct B str_B;
} the_struct;
};

A union is like a struct, but can only hold an element at a time.
You access members of unions like members of struct. For example,
in the case above to access the double of a struct B in a struct C,
you'll have to use mystructC.the_struct.str_B.z
Or try to find a better way to do what you're trying to do.

Thanks - I forgot about unions ! (don't use them that much). Yes this
soln will suffice for what I want to do.
Jun 30 '07 #5


Army1987 wrote:

<snip></snip>
struct C {
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;
Why have you declared an enum here? ^ (can't see why this is needed)

union {
struct A str_A;
struct B str_B;
} the_struct;
};
Jun 30 '07 #6

"Grey Alien" <gr**@andromeda.comha scritto nel messaggio news:if******************************@bt.com...
>

Army1987 wrote:

<snip></snip>
>struct C {
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;
Why have you declared an enum here? ^ (can't see why this is needed)
The point is, you have to somehow keep track of wheter the union
contains a struct A or a struct B.

> union {
struct A str_A;
struct B str_B;
} the_struct;
};

Jun 30 '07 #7
Grey Alien wrote:
Army1987 wrote:

<snip></snip>
>struct C {
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;
Why have you declared an enum here? ^ (can't see why this is needed)
So you can record whether this union ...
> union {
struct A str_A;
struct B str_B;
} the_struct;
.... is currently supposed to be an A or a B.
>};
As far as solutions go, I think we don't know what problem
you're trying to solve, only the C context you're trying
to solve it in. It may make a difference.

--
Pressure Hedgehog
The "good old days" used to be much better.

Jun 30 '07 #8
Gray Alien wrote:
>
.... snip ...
>
These structures (not pointers to them), need to be stored in a
another struct:

struct C
{
unsigned id ;
char name ;

DATA_TYPE the_struct ;
};
Look up the use of 'union'.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 30 '07 #9
gw****@aol.com wrote, On 30/06/07 13:25:
On 30 Jun, 13:15, "Army1987" <please....@for.itwrote:
>"Gray Alien" <g...@andromeda.comha scritto nel messaggionews:Z7******************************@bt. com...
<snip>
>>Where DATA_TYPE is EITHER a struct A or struct B.
For compatability with existing libraries, I cannot use a void * (for instance), and cast between struct A and B.
Is there a soln to this problem ?
struct C {
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;
union {
struct A str_A;
struct B str_B;
} the_struct;

};

A union is like a struct, but can only hold an element at a time.
You access members of unions like members of struct. For example,
in the case above to access the double of a struct B in a struct C,
you'll have to use mystructC.the_struct.str_B.z
Or try to find a better way to do what you're trying to do.

To the OP: One way of doing it would be to use a union, as Army1987
has suggested. However, if space isn't an issue, you might do better
including both a struct A and a struct B, and just using the one that
you want. This would seem to have less chance of going horribly wrong,
and might make your code clearer. Is there some reason why this would
not work?

Hope that helps.
If it is only valid to have one or the other then a union expresses the
intent better and if you are accessing the wrong member something has
already gone horribly wrong.
--
Flash Gordon
Jun 30 '07 #10


Army1987 wrote:
"Grey Alien" <gr**@andromeda.comha scritto nel messaggio news:if******************************@bt.com...
>>
Army1987 wrote:

<snip></snip>
>>>struct C {
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;

Why have you declared an enum here? ^ (can't see why this is needed)


The point is, you have to somehow keep track of wheter the union
contains a struct A or a struct B.
Ah, I see. I understand now. Actually it won't be necessary since the
structs themselves contain a 'type' enum
>
>> union {
struct A str_A;
struct B str_B;
} the_struct;
};


Jun 30 '07 #11
Grey Alien wrote:
Army1987 wrote:
>"Grey Alien" <gr**@andromeda.comha scritto nel messaggio
news:if******************************@bt.com...
>>>
Army1987 wrote:

<snip></snip>

struct C {
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;

Why have you declared an enum here? ^ (can't see why this is needed)


The point is, you have to somehow keep track of wheter the union
contains a struct A or a struct B.
Ah, I see. I understand now. Actually it won't be necessary since the
structs themselves contain a 'type' enum
Well, you have to be /careful/, then: you can only reliably get
at that enum (IIRC) if it's either the first field in the structure
(A or B) or a member of that first field.

Safer, I would have said, to have it outside the union -- but
it depends on the /full details of the problem you're trying
to solve/, with which we have not been blessed.

--
Unshriven Hedgehog
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/

Jun 30 '07 #12


Chris Dollin wrote:

<snip></snip>
>
Well, you have to be /careful/, then: you can only reliably get
at that enum (IIRC) if it's either the first field in the structure
(A or B) or a member of that first field.

Safer, I would have said, to have it outside the union -- but
it depends on the /full details of the problem you're trying
to solve/, with which we have not been blessed.
I agree. Actually, I decided to err on the side of caution and use the
type enum field in struct C (external to the related structs).
Jun 30 '07 #13
Gray Alien wrote:
I have two related structs:
struct A
{
int x ;
void * data ;
};

and

struct B
{
int x, y ;
double z ;
void * data1, *data2 ;
};
These structures (not pointers to them), need to be stored in a another
struct:

struct C
{
unsigned id ;
char name ;

DATA_TYPE the_struct ;
};
Where DATA_TYPE is EITHER a struct A or struct B.
typedef union {
struct A;
struct B;
} DATA_TYPE;
Jun 30 '07 #14
...
On Sat, 30 Jun 2007 15:05:51 GMT, Chris Dollin
<eh@electrichedgehog.netwrote:
Grey Alien wrote:
Army1987 wrote:
<snip>
The point is, you have to somehow keep track of wheter the union
contains a struct A or a struct B.
Ah, I see. I understand now. Actually it won't be necessary since the
structs themselves contain a 'type' enum

Well, you have to be /careful/, then: you can only reliably get
at that enum (IIRC) if it's either the first field in the structure
(A or B) or a member of that first field.
Actually it's safe if it is in (or within) a common initial sequence
of fields with compatible types. But in practice first is the easiest
way to satisfy this, and almost always desirable anyway.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Jul 22 '07 #15

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

Similar topics

5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
10
by: Diego F. | last post by:
Hello. I need to store custom objects in a SQL Server 2000 table. Which is the easiest way to do it? Do I need to write methods to store each attribute separately from C# app to the table and the...
5
by: Joe | last post by:
Coming from C++ I'm so used to being able to use pointers in Tag properties that I'm a little lost. I want to assign an instance of a class to a Tag property and be able to change one of it's...
2
by: Jacek Dziedzic | last post by:
Hello! Suppose I have a class that contains only public members of builtin types and a default constructor. Because of the default constructor it is no longer an aggregate and therefore no...
14
by: Stainless | last post by:
I have a public class Globals, which obviously holds all my global data. I have an array of 243 items, currently structs of type typedef struct STAR { int x; int y; int stellar_class;...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
14
by: Archanak | last post by:
Hi, I have 13.8 GB of data. The data is of the following:- 1: Proc Natl Acad Sci U S A. 2003 Aug 19;100(17):9768-73. Epub 2003 Jul 30. Characterization of myotubularin-related protein 7...
5
by: major | last post by:
Hi all, I would like to store (and read) a list of structs into a file using c+ + (each struct contains integers and strings), and I'm wondering if it there is a method to do it at "high level", I...
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?
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:
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.