Archived

This topic is now archived and is closed to further replies.

RenegadeKoolAid

Dynamic struct array allocation inside another struct

Recommended Posts

Im having a little problem here, I have two structs, such as:
struct Pos
{
  int x;
  int y;
};

struct Stats
{
   int stuff;
   float stuff;

   Pos *position;
};

Stats *stats;
   
similar to that anyway. Now I allocate my "stats" struct array, and then need to allocate the position struct array inside to have an array of positions for this object to move to. I do it like this:
stats = new Stats[50];           //50 objects or so
stats->position = new Pos[10];   //10 positions for this object

Then try to access the position array in a loop such as:

for( int i = 0; i < 50; i++ )
{
    for( int j = 0; j < 10; j++ )
    { 
       stats[i]->posision[j]->x = 1;
       stats[i]->position[j]->y = 10;
    }
}
   
Im pretty sure this is how to do it, although i get a "does not have an overloaded member operator ->" and "left of '->x' must point to class/struct/union" errors. Anyone know whats going on? The structs are in a class, btw. [edited by - RenegadeKoolAid on July 26, 2003 7:50:55 PM] [edited by - RenegadeKoolAid on July 26, 2003 7:51:33 PM]

Share this post


Link to post
Share on other sites
The subscript operator dereferences the pointer, so you use the . operator, instead of the -> operator:

stats[ i ].position[j].x = 1;
stats[ i ].position[j].y = 10;

Note: If you remember, try to put a space like [ i ], because without spaces the forum interprets it as an italics tag.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites