Use variable to specify structure field

This is a discussion on Use variable to specify structure field within the C Programming forums, part of the General Programming Boards category; Hi, as a brief outline, I'm using a function to sort some data, held in an array of structures. Depending ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    Use variable to specify structure field

    Hi, as a brief outline, I'm using a function to sort some data, held in an array of structures. Depending on what is passed to the function, it will need to sort based on a different field.

    i.e. if "field1" is passed to the function, the function will sort based on structure.field1. If "field2" is passed, it will be sorted based on structure.field2.

    The problem I'm having is using a variable to specify the structure field. How can I set it so that the code effectively reads

    structure.variablevalue

    depending on what is passed to the function as variablevalue.

    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Are all the fields the same type?

    What are you trying to do?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    im sorting an array of structures. there are 5 fields - one long and 4 ints. The structures will be ordered in the array by one of the 4 int fields in them, and the user selects which one to sort by. Basically, I want to be able to pass this

    fieldvariable = "field1";


    ....

    then refer later in code to the field denoted by the variable

    e.g.

    if fieldvariable is as above, I could have

    Code:
    structure.fieldvariable
    would be seen by the program as
    Code:
    structure.field1

  4. #4
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    you will need to pass the address of the structure and then the offset of the member to be sorted.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Could you imagine for a second that I am the complete novice/idiot that I am, and give me the big print version with pictures (and preferably sample code)?Lol. Cheers for the rapid response!

  6. #6
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    edit: that was a failure, just use gcc (dunno if you can with visual studio/cl) and offsetof
    edit: If you're using cl and it doesn't have offsetof or vs doesn't
    Code:
    #define offsetof(type, field)    ((int) (& (((type *) 0)->field)))
    edit: edit: edit:
    sorry, I dropped the code
    Code:
    int main(...)
    {
        ...
        struct structType *aStruct;
        AccessArbitraryField(aStruct, offsetof(structType, fieldName);
        ...
    }
    
    AccessArbitraryField(void *aStruct, int fOff)
    {
        access it as *(aStruct + fOff)
    }
    I think that should work
    Last edited by valis; 03-17-2006 at 05:10 PM.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Maybe change your structure values to an array of ints and pass in the index to sort on.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    So if this were say excel, you'd be looking for some kind of "sort by column" feature?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    @Valis, I'm trying to get your code to work, but it complains at me for using the (aStruct + fOff) code, apparently "void" is not suitable for arithmetic.

    @WaltP, I don't think I can do that, please re-read my first post, if I'm wrong, could you explain what you mean?

    @Salem, yes exactly. I'm passing the array of structures to a bubble sort routine.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Code:
    typedef enum {
      FT_INT,
      FT_LONG,
      FT_DOUBLE,
      FT_STRING,
      FT_MAX
    } cellType;
    
    typedef struct {
      cellType  type;
      union {
        int     iField;
        long    lField;
        double  dField;
        char   *sField;
      } data;
    } cell;
    The whole sheet would be
    Code:
    cell sheet[10][5];
    Setting an item would be
    Code:
    sheet[0][0].type = FT_INT;
    sheet[0][0].data.iField = 123;
    When it comes to sorting, you look at the type field and choose the appropriate comparison.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by Rick87
    @WaltP, I don't think I can do that, please re-read my first post, if I'm wrong, could you explain what you mean?
    You said
    i.e. if "field1" is passed to the function, the function will sort based on structure.field1. If "field2" is passed, it will be sorted based on structure.field2.
    My idea:
    i.e. if "1" is passed to the function, the function will sort based on structure.field[1]. If "2" is passed, it will be sorted based on structure.field[2].

    Turn .fieldn into an array .field[n]
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printf: somehow it changed a structure field.
    By Artemiy in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 11:27 AM
  2. Problem with structure with a float variable
    By j.sreejit in forum C Programming
    Replies: 9
    Last Post: 12-29-2006, 12:23 AM
  3. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  4. Replies: 12
    Last Post: 10-14-2003, 10:17 AM
  5. Variable is not a structure and Sort char **
    By ChazWest in forum C Programming
    Replies: 1
    Last Post: 03-08-2002, 08:40 AM

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