sending an array to a pointer

What I am trying to do is point to a multidimensional array. I found that I can use a pointer to point to a 1-dimensional array. Is it possible to point to a multidimensional array? This is what I have so far:
1
2
3
4
5
6
int var1[5] = {1, 1, 2, 3, 5};
int var2[2][5] = {{1, 2, 3, 4, 5},{6, 7, 8, 9, 10}};
int * var3;
var3 = var1;
//var3 = var2;// this is what I want, but it creates errors
var3 = var2[0];//this works, but it only gives me a section of the array 


Otherwise is there a way to store a multidimensional array in another multidimensional array with something like array1 = array2;?
Yes, but it's ugly:

1
2
3
4
5
6
7
int var1[5] = {...};
int var2[2][5] = {...};

int (*ptr)[5];

ptr = &var1;  // OK
ptr = var2; // OK 


Note that this will only work if the lower bound array has exactly 5 elements. It also will not work with dynamic arrays:

1
2
3
4
5
6
int (*ptr)[5];

int* dynamic = new int[5];

// ... there is no way for ptr to point to dynamic!
//   int[5] and int* are different types. 


(EDIT: actually, dynamic arrays might work with casting....)
(EDIT2: but only if the dynamic array is one dimentional)
Last edited on
thanks, that works!
Topic archived. No new replies allowed.