Thread: filling Structure dynamically

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

    filling Structure dynamically

    I have a Struct containing about a 300 variables
    Code:
    struct Test 
    { 
       int variable1;
       int variable 2;
        .
        .
        .
       int variable300;
    
    }
    and i have a string containing all the values needed to fill the struct.
    is there a way to dynamically fill the data without having to set them as
    Code:
     Test.variable1 = String1;
     Test.variable2 = String2;
                        .
                        .
                        .
    Test.variable300 = String300;
    can we loop through the structures variables and fill them ?

    Thx

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yeah, there is:
    Code:
    struct Test
    {
        int variable[300];
    };
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by alaash
    I have a Struct containing about a 300 variables
    "That is why you fail." - Yoda

    Why don't you use an array or some container?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    actually the structure doesn't have the same variable type , it has arrays , and different variable types. in the example above , i was just trying to deliver the idea behind the question.
    Anyways, i found the solution it can be done using union to merge to structs (the original structure , and another one containing just an array) where they will both write to the same memory thereby writing to the second struct will modify the first.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by alaash View Post
    Anyways, i found the solution it can be done using union to merge to structs (the original structure , and another one containing just an array) where they will both write to the same memory thereby writing to the second struct will modify the first.
    That won't work if the compiler inserts padding into structs. Practically, most compilers introduce padding by default for performance reasons, and the amount of padding between two consecutive elements in a struct is compiler-dependent. And that's assuming your structs are PoD (Plain Old Data). There is virtually no way your approach will work for non-PoD types, or if any of the struct members are non-PoD.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You could put all those members into a std::tuple ... but for 300 different types , it is somewhat unrealistic to write.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by manasij7479 View Post
    You could put all those members into a std::tuple ... but for 300 different types , it is somewhat unrealistic to write.
    and compiling it would likely take hours. I seem to recall that compile time for tuples increases exponentially as you add more template parameters.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by alaash
    actually the structure doesn't have the same variable type , it has arrays , and different variable types. in the example above , i was just trying to deliver the idea behind the question.
    Well, okay. What on earth (or outside of it) is this structure?

    Besides, if you can bear the tedious effort of declaring all those members, you can bear the tedious effort of assigning to them individually.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Out of curiosity: What is this überstruct you're working with. I don't think I ever saw a struct, object or what have you with more than maybe 20 instance variables...

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by alaash View Post
    I have a Struct containing about a 300 variables
    Okay I'm going to stop you right there. Whatever you were going to ask after that point is not particularly important. This is the primary problem right here.
    300 is beyond excessive. That's beyond the 1 in a million chance that you might have stumbled upon the one situation where it actually makes sense to do something a certain unusual way. Straight up, that is not how to program, least of all how to program in C++.
    You may as well be asking how to clean an entire airport runway with a toothbrush. We can't answer such a question except by suggesting that you need a different approach.

    That said, you'll now need to give more information about what is in this structure. Also, how is this structure used? Is it a singleton, or are there many instances? Is it used polymorphically, or to represent more than one possible thing?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help filling a structure with data from a file.
    By System_159 in forum C++ Programming
    Replies: 41
    Last Post: 09-27-2006, 06:39 PM
  2. Filling a structure
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 12-05-2005, 04:00 AM
  3. Quick way of filling structure?
    By voodoo3182 in forum C Programming
    Replies: 3
    Last Post: 08-05-2005, 07:28 AM
  4. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 06:05 PM
  5. dynamically allocate a structure
    By dgraver01 in forum C Programming
    Replies: 3
    Last Post: 07-11-2002, 02:23 PM