3

So I want to initialize an int 2d array very quickly, but I can't figure out how to do it. I've done a few searches and none of them say how to initialize a 2D array, except to do:

int [SOME_CONSTANT][ANOTHER_CONSTANT] = {{0}};

Basically, I've got 8 vertices, and I'm listing the 4 vertices of each face of a cube in an array. I've tried this:

int[6][4] sides = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};

But that tells me that there's an error with 'sides', and that it expected a semi-colon. Is there any way to initialize an array quickly like this?

Thanks!

0

7 Answers 7

8

You have the [][] on the wrong side. Try this:

int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};

Keep in mind that what you really have is:

int **sides

(A pointer to a pointer of ints). It's sides that has the dimensions, not the int. Therefore, you could also do:

int x, y[2], z[3][4], ...;
Sign up to request clarification or add additional context in comments.

Comments

7

I think You meant to say

int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};

1 Comment

I thought I tried doing that. I guess I must've had some other issue at the same time. Thanks!
4

int array[n][m] behaves just like int array[n * m].

In fact, array[i][j] = array[m * i + j] for all i, j.

So int array[2][3] = {1, 2, 3, 4, 5, 6}; is a valid declaration and, for example,

array[1][1] = array[3 * 1 + 1] = array[4] = 5.

1 Comment

Whoa, really? That makes passing 2d arrays to a function 100 times easier!
2
int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}}; 

I'm not a regular c++ programmer but I looks like int sides[6][4] seems to compile while int[6][4] sides fails. Languages like C# lets you have the [][] on either sides but apparently c++ doesn't.

Comments

1

int sides[6][4] = ... should do the trick. This sounds like you may be coming from a Java (or other language) background so I do recommend a C++ book The Definitive C++ Book Guide and List for more details.

Comments

0

Yes, the intended type of sides is int[6][4], but C++ has confusing syntax sometimes. The way to declare said array is:

int sides[6][4] = {/*stuff*/};

You run into this with function pointers too, but even worse:

int (*myfuncptr)(int); //creates a function pointer called myfuncptr

With function pointers though, you can do this:

typedef int (*func_ptr_type)(int);
func_ptr_type myfuncptr;

Unfortunately, there's no corresponding magic trick for arrays.

Comments

-1

i would make a array outside of function and just assign it it to your local. this will very likely invoke memcpy or just inline memory copying loop

this is the fastest you can get

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.