Tableau

coockiesch Messages postés 2268 Date d'inscription mercredi 27 novembre 2002 Statut Membre Dernière intervention 13 septembre 2013 - 23 mars 2003 à 19:32
CoreBreaker Messages postés 540 Date d'inscription dimanche 23 mars 2003 Statut Membre Dernière intervention 5 octobre 2007 - 26 mars 2003 à 02:34
Bonjour à tous.
Je cherche à créer un tableau à deux dimensions de manière dynamique. J'y arrive avec un tableau unidimensionnel mais pas avec deux dimensions... Pourquoi? Comment faire????

Merci

RGug

4 réponses

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
23 mars 2003 à 22:00
de 36 manieres differentes, en voile une:
typedef struct _TAB {
int a;
int b;
}

TAB *ptab;
ptab = (TAB*) malloc(nbr * sizeof(TAB));
On fait ce qu'on veut, improvise.
BruNews, ciao...
0
cs_vieuxLion Messages postés 455 Date d'inscription samedi 26 octobre 2002 Statut Membre Dernière intervention 6 avril 2004 8
23 mars 2003 à 22:11
bonjour

voici un exemple de tableaux à 2 dimensions
en allocation sur la pile et en dynamique
Attention en dynamique à bien TOUT libérer

#include
using namespace std;

int main()
{
{
//alloc sur la pile
int tab2D[2][3];//2 lignes et 3 colonnes
for (int l=0; l<2; l++)
for (int c=0; c<3; c++)
{
tab2D[l][c]=10*l+c; //initialisation
cout << "tab2D["<<l<<"]["<<c<<"]="<<tab2D[l][c]<< endl; //impression
}
}
{
//allocation dynamique
int ** tab2DD = new int* [2];//les deux lignes (des pointeurs)
for (int lig=0; lig<2; lig++)
tab2DD[lig] = new int [3];//les 3 colonnes
//initialisation
for (int l=0; l<2; l++)
for (int c=0; c<3; c++)
{
tab2DD[l][c]=10*l+c; //initialisation
cout << "tab2DD["<<l<<"]["<<c<<"]="<<tab2DD[l][c]<< endl; //impression
}
//destruction
for (l=0; l<2; l++)
delete [] tab2DD[l];
delete[] tab2DD;
}
return 0;
}
0
coockiesch Messages postés 2268 Date d'inscription mercredi 27 novembre 2002 Statut Membre Dernière intervention 13 septembre 2013 4
24 mars 2003 à 11:45
Merci. :-)
Rafael Guglielemtti
0
CoreBreaker Messages postés 540 Date d'inscription dimanche 23 mars 2003 Statut Membre Dernière intervention 5 octobre 2007 1
26 mars 2003 à 02:34
type **_tab= malloc(colonnes * lignes * sizeof(**tab);
type tab[colones][lignes]= _tab;

6ème colone, 4ème ligne: tab[5][3]

ou
type *tab= malloc(colonnes * lignes * sizeof(**tab);

x-ème colone, y-ème ligne: tab[(x*colonnes)+y]

Core Breaker :)
0
Rejoignez-nous