Thread: Using Variable To Read Structure

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    4

    Using Variable To Read Structure

    I have a structure with 10 plus items. For simplified purposes im just going to use 2.

    I am trying to read the items from the structure using a variable and a loop.

    Code:
    for (unsigned i=0);i<2;i++) {
         std::cout << structname->item + i << std::endl;
    }
    is there any way of reading it like this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean you have something like this:
    Code:
    struct blah {
       char name[20];
       int something;
       double someother;
    ....
    };
    And now you want to read all the different fields using a loop?

    That's not trivial, and if they are different types (e.g. a mix of integer, char array, C++ strings, floating point), you are going to struggle to get the language to understand what you are doing.

    If you describe a bit more what your structure looks like, maybe we can suggest something.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    4
    The structure is going to be all std::strings. Yes i know it can be used in a array, but in this case i must use a structure.

    Code:
    struct blah {
       std::string item1;
       std::string item2;
       std::string item3;
       std::string item4;
       std::string item5;
    ....
    };

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Realisticly, if you use a structure, then you have to read each item by itself. There are ways that you could for example build a list of offsets to the data in your structrure, and use a pointer to get the address of each element, but that would really make for some hard reading in the code, and it probably won't really help you do anything useful.

    Of course, you could put an array inside the struct, if that's what is meaningfull to your code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    4
    Thanks, i can't do it the way that i wish. I have to come up with something else.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One way, of course is something like this:

    Code:
    struct blah {
       std::string item1;
       std::string item2;
       std::string item3;
       std::string item4;
       std::string item5;
    ....
    };
    ...
    blah var;
    std::string *arr[] = 
    { 
       &var.item1, 
       &var.item2, 
       &var.item3, 
       &var.item4, 
       &var.item5, 
    ...
    };
    
    ... 
       for(i = 0; i < N; i++)
           cin >> *arr[i];
    ...
    Does that make sense to you?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why not
    Code:
    struct blah
    {
    std::string item[5];
    } mystruct;
    
    for (int i = 0; i < 5; i++)
    mystruct->item[i];
    ?
    It IS possible to access each member of a struct not by its name, if all members are the same size and type, but that involves tricky memory manipulation and pointers.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And it relies on platform characteristics. It's not guaranteed by the standard to work.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Why is it that you cannot use an array within the structure? Otherwise we're just beating around the bush.
    This is beating around the bush:
    Code:
    struct bush
    {
       std::string item1, item2, item3, item4;
       std::string & beat_around(unsigned short index)  //we'll base it at zero, like an *cough* array
       {
          if(index > 4) throw SUX0R;
          switch(index)
          {
             case 0: return item1;
             case 1: return item2;
             case 2: return item3;
             case 4: return item4;
             default: std::assert(no_way);
          }
       }
    };
    
    int main()
    {
        bush george;
        fill_items(george);
        std::cout << "Bush's third item: " << george.beat_around(2) << std::endl;
    
        return 0;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    And it relies on platform characteristics. It's not guaranteed by the standard to work.
    I can't see how it's that. The compiler will create the structure and the compiler will keep track of where the members are positioned and the compiler will generate correct code to access them all. Otherwise arrays are useless.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps
    Code:
    std::map< std::string, std::string > thing;
    then later on, you can do things like
    Code:
    thing["item1"] = "Barney";
    Of course, both things can also be other string variables if you want.
    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.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    And it relies on platform characteristics. It's not guaranteed by the standard to work.
    Which post is this referring to, and in what way do you mean that it's not guaranteed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    To the one by Elysia, directly above mine. She says something about accessing struct members by pointer instead of by name. That doesn't work portably, because the compiler may insert any amount of padding between the members.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    To the one by Elysia, directly above mine. She says something about accessing struct members by pointer instead of by name. That doesn't work portably, because the compiler may insert any amount of padding between the members.
    Ah, it's tricky, but it can be done. Usually, the compiler pads to 4-byte boundaries, but you can force it to not to do that. And if you know that it aligns them to 4-byte boundaries, then it's still possible to do it with pointers given the type is 4 bytes, or you read X bytes, then skip Y bytes = 4 bytes.
    But arrays are different. They should work wherever.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Ah, it's tricky, but it can be done. Usually, the compiler pads to 4-byte boundaries, but you can force it to not to do that. And if you know that it aligns them to 4-byte boundaries, then it's still possible to do it with pointers given the type is 4 bytes, or you read X bytes, then skip Y bytes = 4 bytes.
    That's what CornedBee meant. You can't do it portably; you have to assume things like structure padding. You shouldn't be writing code that relies on this sort of thing, unless you're doing something pretty unusual.

    But arrays are different. They should work wherever.
    Indeed. Array elements have to occupy contiguous positions in memory.

    If you have a structure like this:
    Code:
    struct tag {
        int x1, x2, x3;
    };
    then it's easiest to use an array:
    Code:
    struct tag {
        int x[3];
    };
    On the other hand, if the members are of different types, then you can't create an array out of them. You could create a container to hold the different types, but whatever you're passing them off to has to be able to handle the different types of the members.

    Anyway, just try an array and see how things go.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2009, 04:21 AM
  2. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  3. Read multiple data for one declared variable
    By c++dummy in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2007, 02:13 PM
  4. Use variable to specify structure field
    By Rick87 in forum C Programming
    Replies: 10
    Last Post: 03-19-2006, 01:17 PM
  5. How to read from a file into a structure?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 05-28-2002, 11:56 AM