Home > Back-end >  Two-dimensional array name can cast type assigned to a pointer?
Two-dimensional array name can cast type assigned to a pointer?

Time:10-30

Such as
Int * p;
Int a [3] [4].
P=(int *) a;
If can can explain the appearance of the converted, if can't can you say why the

CodePudding user response:

 # include & lt; stdio.h> 
Int * p;
Int a [3] [4].
int i,j,k;
Unsigned char * b;
Int main () {
for (i=0; i<3; I++)
for (j=0; J<4. J++)
A [I] [j]=I * 4 + j;
B=(unsigned char *) & amp; A [0] [0];
Printf (" \ n b: % p - ", b);
For (k=0; K<3 * 4 * sizeof (int); K++) printf (" % 02 x "[k]. B);
P=(int *) a;
Printf (" \ n p: % p - ", p);
For (k=0; K<3 * 4; K++) printf (" % x "08, p [k]);
return 0;
}
//
//b: 00 cd87b4-00 00 00 00 00 00 00 00 00 00 02 03 01 00 00 00 04 00 00 00 05 00 00 00 06 07 00 00 00 00 00 00 00 00 00 08 09 00 00 00 0 a 00 00 00 00 00 00 0 b
//p: 00 cd87b4-00000000, 00000001, 00000002, 00000003, 00000004, 00000005, 00000006, 00000007, 00000008, 00000009, 0000000 a, 0000000 b

CodePudding user response:

 int * p; 
Int a [3] [4].
P=(int *) a;//that can't be, see if the compiler can let through
P=& amp; A [0] [0];//this is no problem, p points to the first element of the first address, so can access 3 x 4 p=12 elements
  • Related