Results 1 to 5 of 5

Thread: Array of Struct??

  1. #1

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228

    Arrow Array of Struct??

    I can't seem to figure out how to make an array of a user defined struct. I define my struct, like so:
    Code:
    struct MYSTRUCT {
        float x;
        const char *Name;
    };
    The i try to allocate an array like this:
    Code:
    MYSTRUCT MyStruct1 = new MYSTRUCT[ 24 ];
    Several errors come up and I don't know what to do. I am also trying to define an array of structs/unions inside of another struct the same way... error also occurs there. HELP????
    To protect time is to protect everything...

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    I'm not sure, since I haven't programmed in C for a short while, but how about this:
    struct MYSTRUCT MyStruct1[24];

    ???

  3. #3

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    This is C++ not C... and the problem with that is that i have to allocate the array when i declare the variable and i dont want to do that...
    To protect time is to protect everything...

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    riis thing should work without "struct" before it. If you want to have an array of unknown/variable length then you use a dynamic array, in your example MyStruct1* instead of MyStruct1. Easiest would be to use vector from STL: vector<MYSTRUCT> MyStruct1;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Zaei
    Guest
    It should be:
    Code:
    MYSTRUCT* MyStruct1 = new MYSTRUCT[ 24 ];
    You forgot to make the MyStruct1 a pointer variable. Remember to delete [] MyStruct1 when you are done. If you want to declare a standard array of MYSTRUCT, the syntax is:
    Code:
    MYSTRUCT MyStruct1[24];
    Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width