Thread: Can you create an array of structures?

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

    Question Can you create an array of structures?

    I need to be able to to store two strings in a structure and then have 10000 of these structures. Is there a way to do this?

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Yes. Here is one way:
    Code:
    struct MyStruct
    {
        char String_One[255];
        char String_Two[255];
    };
    
    MyStruct myStruct[10000];
    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    and...

    If you have basic knowledge in pointers:
    Code:
    typedef struct
    {
        char String_One[255];
        char String_Two[255];
    }MyStruct;
    
    MyStruct* pObject;
    int NumberOfElements=10000;
    pObject=new MyStruct[NrOfElements];
    
    ...
    
    delete[] pObject;
    This method is great if you want to decide how many structures you want when running the program, not just when compiling.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    typedef unsigned char BYTE;


    Simple array of structures
    struct RGB
    {
    BYTE red;
    BYTE grn;
    BYTE blu;
    BYTE alpha;
    };

    RGB *Palette=new RGB[255];

    Complex array of structures
    struct Point
    {
    int x;
    int y;
    };

    struct Vertex
    {
    double x;
    double y;
    double z;
    };

    struct Coord
    {
    Vertex Local;
    Vertex World;
    Vertex Aligned;
    Point Screen;
    RGB color;
    };

    Coord *Object=new Coord[numvertexes];

    Can also be done with classes.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Oooooooops!!! Hate it when the subject of the post does not exactly match the question. But, ........uh..........yes you can create arrays of structures.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing array of structures causing bus error
    By Aaron M in forum C Programming
    Replies: 5
    Last Post: 03-05-2006, 01:40 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 03:04 AM