3

Possible Duplicate:
C: create a pointer to two-dimensional array

When an array is defined, as

int k[100];

it can be cast to int*:

int* pk = k;

It there a pointer variable a multidimensional array can be cast to?

int m[10][10];
??? pm = m;
Community
  • 1
  • 1
Evgeny
  • 2,121
  • 1
  • 20
  • 31

3 Answers3

5
int m[10][20];
int (*pm)[20] = m; // [10] disappears, but [20] remains

int t[10][20][30];
int (*pt)[20][30] = m; // [10] disappears, but [20][30] remain

This is not a "cast" though. Cast is an explicit type conversion. In the above examples the conversion is implicit.

Not also that the pointer type remains dependent on all array dimensions except the very first one. It is not possible to have a completely "dimensionless" pointer type that would work in this context, i.e. an int ** pointer will not work with a built-in 2D array. Neither will an int *** pointer with a built-in 3D array.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

Yes ofcourse , You can have pointer to multidimentional array.

int m[10][10];
int (*pm)[10] = m;
Omkant
  • 9,018
  • 8
  • 39
  • 59
-2

How about this:

    int k[100];
int* pk = k;
int m[10][10];
int **ptr = (int **) malloc(10 * sizeof(int*));
for(int i=0;i<10;i++)
{
    ptr[i] = m[i];
}
Echilon
  • 10,064
  • 33
  • 131
  • 217
Debobroto Das
  • 834
  • 6
  • 16