Multidimensional array - int a[N][M];
It is good to use when the size is known at compile time. Example:
int a[2][3]; int i, j; for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) a[i][j] = (i + 1) * (j + 1); } for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) printf("%d ", a[i][j]); printf("\n"); }
Memory for such an array is released automatically after the completion of the execution of the block of code where it was declared.
Array of pointers to a pointer - int ** a;
It is good to use when the size of the array is not known at compile time. Example:
int ** a; int N = 2, M = 3; //ΠΠΎΠΏΡΡΡΠΈΠΌ ΡΡΠΈ Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΡΡΠΈΡΡΠ²Π°ΡΡΡΡ ΠΎΡΠΊΡΠ΄Π° ΡΠΎ Π²ΠΎ Π²ΡΠ΅ΠΌΡ ΡΠ°Π±ΠΎΡΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ... int i, j; a = calloc(N, sizeof(int*)); //Π²ΡΠ΄Π΅Π»Π΅Π½ΠΈΠ΅ ΠΌΠ°ΡΡΠΈΠ²Π° ΡΠΊΠ°Π·Π°ΡΠ΅Π»Π΅ΠΉ. for (i = 0; i < N; i++) a[i] = calloc(M, sizeof(int)); //Π²ΡΠ΄Π΅Π»Π΅Π½ΠΈΠ΅ ΠΌΠ°ΡΡΠΈΠ²Π° int'ΠΎΠ², Π² ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ΅ ΠΌΠ°ΡΡΠΈΠ²Π° ΡΠΊΠ°Π·Π°ΡΠ΅Π»Π΅ΠΉ. for (i = 0; i < N; i++) { for (j = 0; j < M; j++) a[i][j] = (i + 1) * (j + 1); } for (i = 0; i < N; i++) { for (j = 0; j < M; j++) printf("%d ", a[i][j]); printf("\n"); } for (i = 0; i < N; i++) { free(a[i]); //ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΠ΅ ΠΌΠ°ΡΡΠΈΠ²Π° int'ΠΎΠ² Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ ΡΠΊΠ°Π·Π°ΡΠ΅Π»Π΅ΠΉ } free(a); //ΠΎΡΠΎΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ ΠΌΠ°ΡΡΠΈΠ²Π° ΡΠΊΠ°Π·Π°ΡΠ΅Π»Π΅ΠΉ
float **arrayis an array of pointers, not a two-dimensional array - Pavel Gridin