J'écris un programme C mais j'ai toujours des problèmes avec mon tableau de caractères. Je continue à obtenir des ordures quand je l'imprime en utilisant prinf. voici un exemple de ce que je reçois quand je l'imprimer:comment se débarrasser des déchets dans un tableau de caractères?
omble chevalier à t.symbol est Aôÿ¿
omble chevalier à tabl [0] .symbol est un
omble chevalier à tabl [1] .symbol est un
carbonisation à tabl [2] .symbol est un
carbonisation à tabl [3] .symbol est d
carbonisation à tabl [4] .symbol est e
carbonisation à tabl [5] .symbol est f
char à tabl [6] .symbol est g
char à tabl [7] .symbol est h
char à tabl [8] .symbol est i
carbonisation à tabl [9] est .symbol x
carbonisation à t [0] .symbol est a0AÃ
carbonisation à t [1] .symbol est b) @ Ã4
carbonisation à t [2] .symbol est CKU *
carbonisation à t [3] .symbol est Aôÿ¿
carbonisation à t [4] .symbol est aller
pourrait Quelqu'un me dit comment se débarrasser de la poubelle dans la gamme de caractères?
ici est mon code
#define MAX 100
#ifndef SYMBSIZE
#define SYMBSIZE 1
#endif
typedef struct tableme
{
char symbol[SYMBSIZE];
int value;
int casenmbr;
int otherinfo;
}tabletype;
int main(int argc, char **argv)
{
tabletype t[MAX];
t[3].symbol[0] = 'A';
t[0].value=1;
t[0].casenmbr = 7;
t[0].otherinfo = 682;
tabletype tabl[MAX];
tabl[0].value = 1;
tabl[0].symbol[0] = 'A';
tabl[1].value = 11;
tabl[1].symbol[0]= 'a';
tabl[2].value = 12;
tabl[2].symbol[0] = 'a';
tabl[3].value = 13;
tabl[3].symbol[0] = 'd';
tabl[4].value = 14;
tabl[4].symbol[0] = 'e';
tabl[5].value = 15;
tabl[5].symbol[0] = 'f';
tabl[6].value = 16;
tabl[6].symbol[0] = 'g';
tabl[7].value = 17;
tabl[7].symbol[0] = 'h';
tabl[8].symbol[0] = 'i';
tabl[9].symbol[0] = 'x';
t[1].symbol[0] = 'b';
t[0].symbol[0]= 'a';
t[2].symbol[0]= 'c';
t[4].symbol[0]= 'g';
printf("char at t.symbol is %s \n", t[3].symbol);
for(x=0;x<10;x++)
{
printf("char at tabl[%d].symbol is %s \n",x, tabl[x].symbol);
}
int j;
for(j = 0; j<5;j++)
{
printf("char at t[%d].symbol is %s \n",j, t[j].symbol);
}
return 0;
}
merci, ça a résolu mon problème. –
Comme une extension de ce que Gabe a dit, la raison pour laquelle vous obtenez le charabia supplémentaire lorsque vous essayez d'imprimer en tant que chaîne est parce que vos tableaux ne sont pas initialisés. Le charabia que vous voyez est la poubelle qui se trouvait dans la mémoire utilisée par les tableaux quand ils ont été attribués. – bta