Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - casting of a multidimensional array [duplicate]

Tags:

c

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;
like image 326
Evgeny Avatar asked Feb 04 '26 22:02

Evgeny


1 Answers

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.

like image 193
AnT Avatar answered Feb 06 '26 12:02

AnT