Sign in to follow this  
Alpha_ProgDes

array of arrays..... both being static

Recommended Posts

Alpha_ProgDes    6935
it is feasible? basically i have two static arrays of enum values. both of different lengths. i want to switch between them. example:
enum Frames {first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth};
static Frames walkSequence[4] = {second, third, first, third};
static Frames shootSequence[6] = {fourth, fifth, sixth, eighth, ninth, tenth}; 

static Frames sequenceChoice[1][2];
sequenceChoice[0][0] = shootSequence;
sequenceChoice[0][1] = walkSequence;
can this work? it seems to compile but when i try to access the arrays my program just terminates automatically. any ideas?

Share this post


Link to post
Share on other sites
Drew_Benton    1861
This worked fine for me, is it what you are looking for?

enum Frames {first = 1, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth};
static Frames walkSequence[4] = {second, third, first, third};
static Frames shootSequence[6] = {fourth, fifth, sixth, eighth, ninth, tenth};

static Frames* sequenceChoice[2];

int main(int argc, char* argv[])
{
sequenceChoice[0] = shootSequence;
sequenceChoice[1] = walkSequence;

std::cout << sequenceChoice[0][0] << std::endl;
std::cout << sequenceChoice[1][0] << std::endl;

return 0;
}





Shows:

4
2
Press any key to continue

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6935
interesting....[

static Frames* sequenceChoice[2];

sequenceChoice[0] = shootSequence;
sequenceChoice[1] = walkSequence;

//this is obviously different from

static Frames sequenceChoice[1][2];

sequenceChoice[0][0] = shootSequence;
sequenceChoice[0][1] = walkSequence;

//and different from

static Frames **sequenceChoice;

sequenceChoice[0] = shootSequence;
sequenceChoice[1] = walkSequence;

i wonder why? unfortunately i don't know the intricates of why your way works and mine doesn't. so a point to a good website would be helpful [smile]

note: again, Drew_Benton saves the day again. your way did work. thanks.

Share this post


Link to post
Share on other sites
raptorstrike    181
r u sure this would compile? i did a remake of this example only with ints and it says invalid convertion from int* to int wich makes sence but if i declare the sequenceChoice to be a "static Frames* NAME[1][2]" then i works, it spits out a weird number, it could be the address to the array of shootSequence but im not sure

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Quote:
Original post by Alpha_ProgDes
i wonder why? unfortunately i don't know the intricates of why your way works and mine doesn't. so a point to a good website would be helpful [smile]


Don't have a site so I'll explain.

You have two arrays - walkSequence and shootSequence. Now you want to be able to access both of them through one array.

In order to access an array without the use of an array, you must use a pointer. This pointer has to point to the address of the array.

So it would look like this:
Frames* ptr1 = walkSequence;
Frames* ptr2 = shootSequence;


Which is the same as:
Frames* ptr1 = &walkSequence[0];
Frames* ptr2 = &shootSequence[0];


Now that is close to what you want, but you want to acces it via one variable instead.

In order to do that you will need to use an array - not just any array though, an array of pointers.

So now I have:

static Frames* sequenceChoice[2];

Which is an array of 2 pointers to a Frame.

Now it is just a matter of assigning the locations:

sequenceChoice[0] = walkSequence;
sequenceChoice[1] = shootSequence;


Then when you want to use it, you use it like a normal array, except it will return the address of the stored array, to which you have to access it like you would usually do:

sequenceChoice[0][0] is the same as having

Frames* ptr1 = walkSequence;
ptr1[0];


So now we just access it like you need to. Note that you must make sure you do not access the array with an outof bounds range - ie:

sequenceChoice[2][0]
sequenceChoice[0][6]
sequenceChoice[1][5]


Will kill your app since you accessing memory that does not belong to the array. If you have any questions on this feel free to ask [smile]

Ok being a STL Nazi, here is a lot more safer way. Just an alternative. I'd not use it though but I'll save someone from just saying "use a vector" [lol]

enum Frames {first = 1, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth};

std::vector< std::vector<Frames> > sequenceChoice;
static std::vector<Frames> shootSequence;
static std::vector<Frames> walkSequence;

int main(int argc, char* argv[])
{
walkSequence.push_back(second);
walkSequence.push_back(third);
walkSequence.push_back(first);
walkSequence.push_back(third);

shootSequence.push_back(fourth);
shootSequence.push_back(fifth);
shootSequence.push_back(sixth);
shootSequence.push_back(eighth);
shootSequence.push_back(ninth);
shootSequence.push_back(tenth);

sequenceChoice.push_back( shootSequence );
sequenceChoice.push_back( walkSequence );

std::cout << sequenceChoice.at(0).at(0) << std::endl;
std::cout << sequenceChoice.at(1).at(0) << std::endl;

return 0;
}






Which shows the same output [wink].

- Drew

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
somewhat corresponding to what drew offered i suggest looking at the boost::array container. you could basically take drew's code and get rid of the overhead caused by the dynamic nature of std::vector.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this