Structure containing a union of more structures!

This is a discussion on Structure containing a union of more structures! within the Windows Programming forums, part of the Platform Specific Boards category; Wow am I confused... 1. When dealing with my structure 'subsectionData' how should I implement the 'sectionType'(cross section)? 2. Do ...

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    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]

    [edit]
    Typo's corrected
    [\edit]
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    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. #3
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Thanks Codeplug!

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

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structure and union
    By BEN10 in forum C Programming
    Replies: 10
    Last Post: 06-24-2009, 11:30 PM
  2. Structure of structures + linked list
    By saeculum in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 07:02 PM
  3. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21