Bonjour, j'ai un prgm qui me calcule ce que je veux, mais si j'affiche la même variable hors de la boucle, elle vaut autre chose.
Le but de mon prgm est de me faire un tableau contenant respectivement "Particule1.txt", "Particule2.txt", "Particule3.txt"
Voici mon code
La sortie obtenue estCode:
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 #include<stdlib.h> #include <stdio.h> #include<string.h> int main() { int j; char * t[3]; for(j=0;j<3;++j) { char s[30]="Particule"; char buf[5]; sprintf(buf,"%d",j+1); strcat(s,buf); strcat(s,".txt"); t[j]=s; printf("%s\n",t[j]); } printf("\n"); printf("\n"); printf("affichage de t\n"); for(j=0;j<3;++j) printf("%s\n",t[j]); return 0; }
Pourquoi une différence. J'ai donc essayer avec des pointeurs, et j'ai le même résultat :Code:
1
2
3
4
5
6
7
8
9
10 Particule1.txt Particule2.txt Particule3.txt affichage de t Particule3.txt Particule3.txt Particule3.txt
Savez-vous pourquoi ?Code:
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 #include<stdlib.h> #include <stdio.h> #include<string.h> int main() { char ** NameFileOutput=malloc(3*sizeof(* NameFileOutput)); int j; for(j=0;j<3;++j) { char s[30]="Particule"; char buf[5]; sprintf(buf,"%d",j+1); strcat(s,buf); strcat(s,".txt"); NameFileOutput[j]=s; printf("%s\n",NameFileOutput[j]); } int i; for(i=0;i<3;++i) printf("%s\t",NameFileOutput[i]); printf("\n"); free(NameFileOutput); }
Merci