const int TAILLE_X = 100;
const int TAILLE_Y = 100;
unsigned int i = 0;
int** tab2d = NULL;
tab2d = new int* [TAILLE_X];
if (tab2d==NULL) return -1;
for (i=0; i<TAILLE_X; i++) {
tab2d[i] = new int [TAILLE_Y];
if (tab2d[i]==NULL) {
// delete tous les précédents
return -1;
}
}
Et voilà :)
Et bien suûr on peut faire la même chose en C, il suffit de remplacer l'utilisation de new et delete par malloc() et free();
# Rien ne vaut un exemple ...
Posté par Florent C. . En réponse au message Tableaux dynamiques multidimmensionnels. Évalué à 5.
const int TAILLE_Y = 100;
unsigned int i = 0;
int** tab2d = NULL;
tab2d = new int* [TAILLE_X];
if (tab2d==NULL) return -1;
for (i=0; i<TAILLE_X; i++) {
tab2d[i] = new int [TAILLE_Y];
if (tab2d[i]==NULL) {
// delete tous les précédents
return -1;
}
}
Et voilà :)
Et bien suûr on peut faire la même chose en C, il suffit de remplacer l'utilisation de new et delete par malloc() et free();