Quick Fill "struct"s

This is a discussion on Quick Fill "struct"s within the C++ Programming forums, part of the General Programming Boards category; Hullo Let's say I Initialise a struct with: Code: struct MyStruct { FLOAT A, B, C; }; Now, when I ...

  1. #1
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361

    Quick Fill "struct"s

    Hullo

    Let's say I Initialise a struct with:
    Code:
    struct MyStruct
    {
    FLOAT A, B, C;
    };
    Now, when I initialise a variable, I can do:
    Code:
    MyStruct MyVariable[]=
    {
    {1, 2, 3}
    {2, 6, 2}
    //And so forth
    };
    What if though, I want to add another data set to the array? Right now MyVariable[0] and MyVariable[1] have info, but MyVariable[2] does not.
    Do I have to do each component seperately?
    I.e.
    Code:
    MyVariable[2].A = 4;
    MyVariable[2].B = 1;
    MyVariable[2].C = 0;
    I've tried:
    Code:
    MyVariable[2]=
    {
    {4, 1, 0}
    };
    //Which gives me many errors about the {} brackets.
    
    //I also tried:
    MyVariable[2] = (4, 1, 0);
    //Which tells me that the Binary "=" sign can't assign the floats (or something to that effect)
    
    //I've also tried other combinations, but none have worked
    So, I guess my question is, do I have to settle for that first one? Doing each element of the structure individually?

  2. #2
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    Either you do it individually, or..

    Code:
    MyStruct blah = { 0, 1, 2 } ;
    
    memcpy(&MyVariable[2],&blah,sizeof(MyStruct)) ;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Code:
    MyStruct MyVariable[]=
    {
        {1, 2 },  // MyVariable[0].C = 0
        {2, }, // MyVariable[1].B = 0, MyVariable[1].C = 0
        //And so forth
    };
    If you have a partially complete initialiser, the remaining elements are filled with zeros of the appropriate type (0 for ints, 0.0 for floats, (T*)0 for pointers of type T).
    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.

  4. #4
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Along the same lines, would I be able to use the memcpy method to copy over an array of "MyStruct"s INTO another array?

    I.e. Given:
    Code:
    MyStruct Temp[3];
    MyStruct All[];
    I'm looking for a faster way to accomplish:
    Code:
    memcpy(&All[1],&Temp[0],sizeof(MyStruct));
    memcpy(&All[2],&Temp[1],sizeof(MyStruct));
    memcpy(&All[3],&Temp[2],sizeof(MyStruct));
    Is there some way I could choose a starting point in the "All" variable and bring over the entire "Temp" variable at once to fill in a chunk of the "All" variable?

    Thanks a ton for the help so far

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    161
    try:
    Code:
    memcpy(&All[1], &Temp[0], sizeof(MyStruct) * number_of_items);
    where number_of_items equals the number of elements that you want to copy.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> MyStruct All[];

    You can't do that, by the way. Either give it a size or initialize it with the number of cells you want.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    I can't?........damn.

    The thing is, I don't know how big I want it to be yet, so I was just planning on adding to it during runtime. And I don't know how much I'll be adding...should I just make it one very massive variable? Cause that seems like a waste of resources....and there's always the problem that, what if I need more than initialised?

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    If you need more than initialized then you can have a pointer to a a datastructer. declared as "
    MyStruct * myvariable = calloc(sizeof(MyStruct), numberofelements);"
    To add an element do this;

    "MyVariable = realloc(MyVariable, numberofelements * sizeof(MyStruct))"
    now to access "MyVariable[element].whatever = whatever"
    need to resize use realloc again and need to delete use free.

    Hope that helps!
    Last edited by Benzakhar; 12-26-2003 at 11:40 AM.

  9. #9
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    I would use

    Code:
    MyStruct * myvariable = (MyStruct *) malloc(sizeof(MyStruct) * numberofelements) ;
    instead of your calloc example. malloc doesn't waste cycles setting everything to 0.

  10. #10
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Thanks for the help guys This thread's getting kinda too close to my thread in the other part of the forum, so I'll change the subject. (But thanks again, it's all been very helpful)


    I have one more question about structures.

    If I declare:
    MyStruct Structures[]={
    {2,5,3},
    {1,5,8},
    };
    (For Instance)

    how am I able to compare various Indices to see if they have data yet?
    I.e.

    if (Structures[3] == NULL)

    something to that effect. (That one doesn't work because C++ says I can't do binary comparison).
    Last edited by Epo; 12-26-2003 at 03:33 PM.

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    You can't do if (Structures[3] == NULL)
    since it isn't a pointer.

    The only way to do it is to add a variable telling if Structures[X] is initalized or using an array of pointers to MyStructs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 11:33 AM
  2. Quick fill question
    By Mr Moe in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2005, 04:26 PM
  3. Replies: 3
    Last Post: 12-22-2004, 07:29 PM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 08:24 PM

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