Thread: Cycling through the fields in a structure

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    40

    Cycling through the fields in a structure

    Is it possible to increment through the fields of a structure using an index value.

    If I had a structure like:

    Code:
    typedef struct {
       int field1;
       int field2;
       int field3;
    } mystruct;
    And say I wanted to populate the fields of the structure within a loop like:

    Code:
    mystruct aStruct;
    
    for ( index = 0 ; index == 3; index++)
    {
          aStruct.index = 10;
    }

    Where index will refer to the field number in the structure?

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Try an array:
    Code:
    typedef struct {
       int field[3];
    } mystruct;
    
    mystruct aStruct;
    
    for ( index = 0 ; index < 3; index++)
    {
          aStruct.field[index] = 10;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    40
    Cant do, there will be string fields in the stucture as well. Which I think answers my question in a slightly different way. Because unless I program in the loop which fields are strings and which are integers, it will not know whether to do a strcpy or an integer assignment, in which case it will probably be better handling each field of the structure sequentially and just having a very long function.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Unless you make each member of your struct into a union containing strings and integers, and something to discriminate which one you have at any moment, then it's lots of code to treat each member of the struct separately.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cycling through structure
    By slippy in forum C++ Programming
    Replies: 9
    Last Post: 04-10-2009, 08:57 PM
  2. bit fields in a structure
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 04-18-2007, 12:10 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. interpreting time fields in a structure
    By *ClownPimp* in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2003, 12:07 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM