Passing a two-dimensional array to a struct (C++) -
i have problem includes pointers , two-dimensional arrays.
i've got struct, looks this:
typedef struct { int row; int col; int **a; } test;
now want use object of type other functions. i'm having problems passing two-dimensional array object of type.
for example tried this:
int main(int argc, char * argv[]){ //just fill array integers int rows = 3; int cols = 3; int a[rows][cols]; srand(time(null)); (int x = 0; x < rows; x++){ (int y = 0; y < cols; y++){ a[x][y] = rand() % 10 + 1; } } test * t = (test *) calloc(1,sizeof(test)); t->row = rows; t->col = cols; t->a = a; return 0; }
how can properly?
i'm thankful help.
if need allocate test object dynamically can this:
int main(int argc, char * argv[]) { //just fill array integers int rows = 3; int cols = 3; test* t = new test; t->row = rows; t->col = cols; t->a = new int*[rows]; for(int = 0; < rows; i++) t->a[i] = new int[cols]; srand(time(null)); (int x = 0; x < rows; x++){ (int y = 0; y < cols; y++){ t->a[x][y] = rand() % 10 + 1; } } return 0; }
Comments
Post a Comment