declaring 2 D array
-
I am getting confused in declaring a 2d array I need a n*4 matrix (n rows with 4 cols in each row) I wanted to do something like __int8 stepArray[][]={{1,2,3,4},{5,6,7,8}}; corresponds to 1 2 3 4 5 6 7 8 where n is implicitly 2 I am getting an error . Please help
-
I am getting confused in declaring a 2d array I need a n*4 matrix (n rows with 4 cols in each row) I wanted to do something like __int8 stepArray[][]={{1,2,3,4},{5,6,7,8}}; corresponds to 1 2 3 4 5 6 7 8 where n is implicitly 2 I am getting an error . Please help
-
I am getting confused in declaring a 2d array I need a n*4 matrix (n rows with 4 cols in each row) I wanted to do something like __int8 stepArray[][]={{1,2,3,4},{5,6,7,8}}; corresponds to 1 2 3 4 5 6 7 8 where n is implicitly 2 I am getting an error . Please help
you can use pointers and memmory allocation to do the trick! try this, it should work, I had an assignment on this, a while ago... Let's see if I can dig up some code :)
__int8 *stepArray[4] = NULL; // declaration
// n should be the number of rows you want, not the highest index!!!
// the best way to insert elements is through a function or to embed this
// array in a class
stepArray = (__int8 **) malloc(n * sizeof(__int8*)); // the number of rows you want
// allocate for each row 4 columns
while(n-- > 0)
stepArray[n] = (__int8*) malloc(sizeof(__int8));after this, you can use it as a normal 2d matrix (stepArray[n][m] =...) It is far more easy to use a vector of some sort, but it's more fun to do it yourself (at least, I experience it that way :)) hope this helps :) ps. I don't want te scare you, but beware of memory leaks, because they sneak in very easy...
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
-
only last dimension can be undefined try __int8 stepArray[2][]={{1,2,3,4},{5,6,7,8}}; better yet use std::vector
Actually, it's the opposite. The first dimension can be undefined, as in:
__int8 stepArray[][4]={{1,2,3,4},{5,6,7,8}};