Thread: Not Sure if this is Possible

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Not Sure if this is Possible

    Hi,

    This probably sounds daft but im trying to keep my code tidy.

    I have a structure defined, like the one below

    Code:
    struct property
    {
    int beds;
    int rooms;
    int property_specific
    }
    struct property house[9];
    struct property flat[4];

    is there a simple way i can also change the name of the property_specific, for example

    currently if i want to access that variable on flat 6 it would be flat[6].property_specific = ........

    ideally i would like to make it so if house is the struct property_ specific is garden and flat it would be balcony, and be accessed something like flat[6].balcony = .........

    im guessing it would be a pointer but im not sure how to point to just that specific variable

    thanks

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I suggest you create two difference structs with the fields you want.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    You could do a union:
    Code:
    enum property { HOUSE, FLAT };
    
    union info {
        int garden;
        int balcony;
    };
    
    struct property {
        int beds;
        int rooms;
        enum property property_type; /* use a value from enum property */
        union info property_specific;
    };
    and use as, for example

    Code:
    struct property item[13];
    item[0].beds = 3; item[0].rooms = 2; item[0].property_type = HOUSE; item[0].property_specific.garden = 42;
    item[1].beds = 1; item[1].rooms = 3; item[1].property_type = FLAT; item[1].property_specific.balcony = 42;

Popular pages Recent additions subscribe to a feed