О различии статических и динамических массивов
$ cat ./test.c
#include «stdio.h>»
#define H 2
#define W 3
int main ()
{
int static_arr[H][W];
int **dynamic_arr = malloc(H * sizeof(int*));
*dynamic_arr = malloc(W * sizeof(int));
int *tmp = malloc(10000 * sizeof(int));
*(dynamic_arr+1) = malloc(W * sizeof(int));
int N = 0;
for(int r = 0; r < H; ++r)
for(int c = 0; c < W; ++c)
{
static_arr[r][c] = N;
dynamic_arr[r][c] = N++;
}
printf("static_arr[1][2]=%d, static_arr=%p, ptr=%p, idx=%ld\n",
static_arr[1][2], static_arr, &static_arr[1][2],
(long)&static_arr[1][2] — (long)static_arr);
printf("dynamic_arr[1][2]=%d, dynamic_arr=%p, ptr=%p, idx=%ld\n",
dynamic_arr[1][2], dynamic_arr, &dynamic_arr[1][2],
(long)&dynamic_arr[1][2] — (long)dynamic_arr);
free(*dynamic_arr);
free(*(dynamic_arr+1));
free(dynamic_arr);
free(tmp);
return 0;
}
$ gcc -std=c99 ./test.c
$ ./a.out
static_arr[1][2]=5, static_arr=0xbf95b564, ptr=0xbf95b578, idx=20
dynamic_arr[1][2]=5, dynamic_arr=0x83bf008, ptr=0x83c8c78, idx=40048$