cs_thespartan
Messages postés36Date d'inscriptionsamedi 3 février 2007StatutMembreDernière intervention26 mai 2008
-
7 juin 2007 à 11:04
SAKingdom
Messages postés3212Date d'inscriptionlundi 7 novembre 2005StatutMembreDernière intervention16 février 2009
-
7 juin 2007 à 14:40
Voilà j'ai cette fonction qui marche :
void alloue(int lig,int col){
int i,j;
int l= 'A';
T = (int **) malloc(col*sizeof(int*));
if (T == NULL)
printf("\nEchec lors de l'allocation memoire!");
for (i=0;i<col;i++) {
T[i] =(int*)malloc(lig*sizeof(int));
if (T[i] == NULL)
printf("\nEchec lors de l'allocation memoire!");
}
}
appelée ainsi : alloue(4,N); . T est déclaré globalement (int **T;)
Ensuite, je choisi de ne plus faire réfénrence à une variable globale dans alloue(4,N); donc j'ajoute un 3ème argument : le tableau dont on veut allouer de la mémoire :
void alloue(int **t,int lig,int col){
int i,j;
int l ='A';
//graphe
t = (int **) malloc(col*sizeof(int*));
if (t == NULL)
printf("\nEchec lors de l'allocation memoire!");
for (i=0;i<col;i++) {
t[i] =(int*)malloc(lig*sizeof(int));
if (t[i] == NULL)
printf("\nEchec lors de l'allocation memoire!");
}
}
appelée ainsi : alloue(T,4,N); .Et ça marche pas . Quelqu'un peut m'aider ??
Spartan
A voir également:
Appel de fontions avec un tableau de pointeurs en paramèmtre
HSylvio
Messages postés116Date d'inscriptionjeudi 22 juillet 2004StatutMembreDernière intervention14 juin 2012 7 juin 2007 à 11:55
Bon yavait longtemps que j'avais pas codé ca m'a fait plaisir de revoir un peu les joies des pionteurs...
Alors voila un truc ressemblant qui marche :
int** alloue(int lig,int col);
int main(int argc, char *argv[])
{
int **t = alloue( 3, 12);
printf("\n%d", 12);
for (int i=0;i<3;i++) {
for (int j=0;j<12;j++) {
printf("\n%d", t[i][j]);
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
int** alloue(int lig,int col){
int i,j;int n=0;
//graphe
int **t;
t = (int **) malloc(col*sizeof(int*));
if (t == NULL)
printf("\nEchec lors de l'allocation memoire!");
for (i=0;i<col;i++) {
t[i] =(int*)malloc(lig*sizeof(int));
if (t[i] == NULL)
printf("\nEchec lors de l'allocation memoire!");
}
printf("\nFini alloc?");
for (i=0;i<lig;i++) {
for (j=0;j<col;j++) {
t[i][j] = n++;
printf("\n%d", t[i][j]);
}
}printf("\nFini rempli?");
return t;
}
Avec ça tu devrais arriver a faire ce que tu veux...non?
cs_thespartan
Messages postés36Date d'inscriptionsamedi 3 février 2007StatutMembreDernière intervention26 mai 2008 7 juin 2007 à 14:09
Ton code m'a bien aidé, merci . Le **t j'aurai du le déclarer dans int** alloue(int lig,int col) et le renvoyer au lieu d'en faire un paramètre de cette fonction. Reste à comprende. Peux-tu m'expliquer?(toi ou quelqu'un d'autre)
PS : ma fonction corrigée, ça peut toujours aider... :
int** alloue(int lig,int col){
int i,j,**t;
t = (int **) malloc(col*sizeof(int*));
if (t == NULL)
printf("\nEchec lors de l'allocation memoire!");
for (i=0;i<col;i++) {
t[i] =(int*)malloc(lig*sizeof(int));
if (t[i] == NULL)
printf("\nEchec lors de l'allocation memoire!");
}
return t;
}
et T=alloue(4,N); sachant que T est une variable globale
Spartan