Thread: Not Sure if this is Possible

  1. #1
    Registered User
    Join Date
    Nov 2011

    Not Sure if this is Possible


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

    I have a structure defined, like the one below

    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


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

  3. #3
    Registered User
    Join Date
    Sep 2012
    You could do a union:
    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

    struct property item[13];
    item[0].beds = 3; item[0].rooms = 2; item[0].property_type = HOUSE; item[0] = 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