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;
0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes ofcourse , You can have pointer to multidimentional array.

int m[10][10];
int (*pm)[10] = m;

Comments

-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];
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.