1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include <stdio.h>
#include <stdlib.h>
char** allouer_espace_memoire(int taille) {
int i, j;
char **tic_tac_toe=NULL;
tic_tac_toe = malloc(taille*sizeof(char*));
if (!tic_tac_toe) goto out;
for (i = 0; i < taille; i++) {
tic_tac_toe[i] = malloc(taille);
if (!tic_tac_toe[i]) {
for (j = 0; j < i; j++)
if (tic_tac_toe[j]) free(tic_tac_toe[j]);
if (tic_tac_toe) free(tic_tac_toe);
tic_tac_toe = NULL;
break;
}
}
out:
return tic_tac_toe;
}
int main(void)
{
char **tic_tac_toe=NULL;
tic_tac_toe = allouer_espace_memoire(10);
if (tic_tac_toe) {
tic_tac_toe[9][9] = 'a';
printf("tic_tac_toe[9][9] = %c\n", tic_tac_toe[9][9]);
}
return 0;
} |