Like Tree2Likes
  • 2 Post By std10093

Not Sure if this is Possible

This is a discussion on Not Sure if this is Possible within the C Programming forums, part of the General Programming Boards category; Hi, This probably sounds daft but im trying to keep my code tidy. I have a structure defined, like the ...

  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,681
    I suggest you create two difference structs with the fields you want.
    laserlight and qny like this.

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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

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