0

So I have a bunch of boolean arrays that I would like to put into a single array for easier accessing, but for some reason this doesn't quite work.

My arrays look like this:

boolean l1_000[8] = {1,0,0,0,0,0,0,0};

I declare my array of arrays with:

boolean level1[8];

And then I figured I could either of these two (first of which just declaring these arrays directly where I set them on the big array):

level1[0] = {1,0,0,0,0,0,0,0};
level1[0] = l1_000;

I also tried level1[8][], but that didn't work either. So what am I doing wrong here? How would I do this?

EDIT: So I managed to do this by declaring the array as boolean *level1[8], but that only allows me to do level1[0] = l1_000. Is there any way I can do level1[0] = {1,0,0,0,0,0,0,0}?

2 Answers 2

5

You can't declare an array of arrays when you want to store arrays that are already created without copying each element, but you can declare an array of pointers:

boolean* level1[] = {
    l1_000, // the array name decays to a pointer to the first element
    l2_000,
    // etc
};
Sign up to request clarification or add additional context in comments.

2 Comments

I just figured that out as well. But how would I do this if I want to create my arrays right there on the same line?
@ChristianA.Strømmen you have to pick one or the other, either storing arrays that have already been created or creating them right there on the same line. If you choose to create them on the same line, do this: boolean level1[][8] = { { 0, 1, 1, 1, 0, 0, 0, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, /* etc */ };
1

My C-fu is kinda weak, but you have to do this:

int l1_000[8] = {1,0,0,0,0,0,0,0}; // one row
int level1[8][8];// 8 high, 8 wide
memcpy(level1[0], l1_000, sizeof(int)*8); // size of int * number of memory elements

basically doing int variable[] will should only be used when you are initializing it right then and there other wise it won't work.

also setting the array position like this

level1[0] = l1_000;

doesn't work because you are trying to set the first element to be the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.