Domanda

Sto scrivendo un programma C ma continuo ad avere problemi con il mio array di caratteri.Continuo a ricevere spazzatura quando lo stampo utilizzando prinf.ecco un esempio di ciò che ottengo quando lo stampo:

il carattere in t.symbol è Aôÿ¿
char in tabl[0].symbol è A
char in tabl[1].symbol è a
char in tabl[2].symbol è a
char in tabl[3].symbol è d
char in tabl[4].symbol è e
char in tabl[5].symbol è f
char in tabl[6].symbol è g
char in tabl[7].symbol è h
char in tabl[8].symbol è i
char in tabl[9].symbol è x
char in t[0].simbolo è a0AÃ
char in t[1].simbolo è b)@Ã4
char in t[2].simbolo è ckU*
char in t[3].simbolo è Aôÿ¿
char in t[4].simbolo è andare

qualcuno potrebbe dirmi come eliminare la spazzatura nell'array di caratteri?

ecco il mio codice

#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;
}
È stato utile?

Soluzione

Hai uno dei due possibili problemi.Se vuoi davvero solo un singolo carattere, dovresti stamparlo con %c, non %s come questo:

printf("char at t.symbol is %c \n", t[3].symbol[0]);

Se vuoi davvero le stringhe devi definirle SYMBSIZE essere 1 superiore alla lunghezza massima di un simbolo e inserire un NUL (\0) carattere alla fine.Puoi farlo in un paio di modi:

  1. tabl[6].symbol[0] = 'g'; tabl[6].symbol[1] = '\0';
  2. strcpy(tabl[6].symbol, "g");

Altri suggerimenti

Il problema è che le stringhe in C hanno la terminazione nulla.Tuttavia, il tuo array è abbastanza grande per un solo carattere, quindi non può avere terminazione null (questo richiede almeno 2 caratteri)

Perché prendi la spazzatura?Quando dici tabl[0].symbol[0] = 'A';, A tabl[0].symbol[0] in memoria hai A??? e non sappiamo cosa ??? È.Ovviamente, a volte è 0 (dove ottieni l'output corretto), a volte non lo è (spazzatura).

Per risolvere questo problema, utilizzare il file %c identificatore di formato per stampare un singolo carattere invece di una stringa con terminazione null.Se vuoi davvero stampare stringhe, devi ingrandire il buffer delle stringhe e terminarlo con null:

SYMBSIZE = 10, quindi avere tabl[0].symbol[1] = '\0'; Nota, di solito vuoi usare le funzioni stringa:

// copy "a" to that memory location, will be null-terminated.
strcpy(tabl[0].symbol, "a"); 

Se il buffer non è abbastanza grande per la stringa, sovraccaricherà il buffer.

I tuoi array non sono abbastanza grandi per memorizzare stringhe.È necessario uno spazio aggiuntivo per il carattere nullo.Cambia SYMBSIZE in 2 o stampalo come carattere.Entrambi dovrebbero funzionare.

MODIFICARE:Inoltre, non inserirai comunque i caratteri nulli.La stampa di una stringa continuerà finché non raggiunge \0, quindi dovresti farlo SYMBSIZE = 2 e poi fare symbol[1] = '\0'; per stampare come una stringa.

EDIT2:O semplicemente cambia il tuo %s ad a %c (stampa un singolo carattere alla volta invece dell'intera stringa)

Per stampare un carattere dovresti usare %c nel printf.

   printf("char at t.symbol is %c \n", t[3].symbol);

Il tuo compilatore dovrebbe avvisarti che la stringa di formato non corrisponde ai valori passati.Gli avvisi del compilatore sono presenti per un motivo.

Se vuoi i caratteri, devi usare %c e non %s per stampare i caratteri.

printf("char at tabl[%d].symbol is %c \n",x, tabl[x].symbol);

C'è un modo per risolvere il problema.Fondamentalmente una volta inizializzato l'array di caratteri, dobbiamo sovrascriverlo con un valore spazzatura su NUll. enter code here

void main()
{
    int amsb[10];
    int i,k,i1;
    char arr[25];
    clrscr();
    printf("Enter the 10 element");

    for( i1=0;i1<25;i1++) {
        arr[i1]='\0';
    }

    for( k=0;k<25;k++) {
        printf("%c",arr[k]) ;

        if(arr[k]=='\0')  {
            break;                
        }
    }

    getch();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top