# Thread: Structure containing a union of more structures!

1. ## Structure containing a union of more structures!

Wow am I confused...

1. When dealing with my structure 'subsectionData' how should I implement the 'sectionType'(cross section)?
2. Do I even need to use a union here?
3. Am I even on the right track?

In my windows prog. I plan on eventually writing my subsectionData to a binary file and in this way when I open a saved file the program will be able to recreate the data. Once I get this part operational I plan on adding a graphical representation on the main window..

Basically, it will be a limited cad program used to calculate moments of inertia, but I wanted to put off the cad portion until I get the rest of my program functional.

Hope this makes sense to you Gurus out there...

[code]
struct sectCircle {
double diameter;
} circle;

struct sectRectangle {
double base;
double height;
} rectangle;

union crossSectionData {
struct circle;
struct rectangle;// many cross sections omitted for clarity
} sectionType;

struct subsection {
crossSectionData sectionType; // circle, rectangle, triangle, parabola, sector, segment, ect...
double area;
double XCoord;
double YCoord;
double Ixc;
double Iyc;
double Ixo;
double Iyo;
double k;
} subsectionData;

struct composite {
double area ;
double XCoord;
double YCoord;
double Ixc;
double Iyc;
double Ixo;
double Iyo;
double k; // (least I / area)^(1/2)
} compositeData;
[\code]

Typo's corrected
[\edit]

2. You need another var in struct subsection, which tells you what member of crossSectionData you ar actually using (a circle or a rectangle or a whatever). I smell an enum...

A few more tips: typedef your structs and unions, then create instances using those typedef's (a good habbit in C):
Code:
```
typedef struct _ mystruct_t
{
...
} mystruct_t;
...
mystruct_t ms;```
gg

3. ## Thanks Codeplug!

Thanks again Codeplug I obviously shouldn't try to program when I am real tired.