1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| int mat[3] = {15, 26, 37};
int (*ptr_mat)[3];
int** dptr_mat;
ptr_mat = &mat;
// version 1: get the adress of int*
// dptr_mat = (int**) &ptr_mat; // <- cast, warning: assignment to int ** from incompatible pointer type int (**)[3]
// version 2: allocate the pointer and copy an int*
dptr_mat = malloc( sizeof(int**) ); // test is NULL
(*dptr_mat) = (int*) ptr_mat; // <- cast, warning: assignment to int * from incompatible pointer type int (*)[3]
printf("%d, %d, %d\n", (*ptr_mat)[0], (*ptr_mat)[1], (*ptr_mat)[2]);
printf("%d, %d, %d\n", (*dptr_mat)[0], (*dptr_mat)[1], (*dptr_mat)[2]);
// printf("%d, %d, %d\n", (**dptr_mat + 0), (**dptr_mat + 1), (**dptr_mat + 2));
// version 2: free
free(dptr_mat); |
Partager