Question

I'm writing a C program but I keep having problems with my array of chars. I keep getting garbage when I print it using prinf. here is an example of what I get when I print it:

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

could someone tell me how to get rid off the garbage in the array of chars?

here is my 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;
}
Was it helpful?

Solution

You have one of two possible problems. If you really just want a single character, you should be printing it with %c, not %s like this:

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

If you really want strings you have to define SYMBSIZE to be 1 higher than the maximum length of a symbol and put a NUL (\0) char at the end. You can do it a couple ways:

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

OTHER TIPS

The problem is that strings in C are null terminated. However, your array is only big enough for one character, so it cannot be null-terminated (this requires at least 2 characters)

Why are you getting garbage? When you say tabl[0].symbol[0] = 'A';, at tabl[0].symbol[0] in memory you have A??? and we don't know what ??? is. Obviously, it's sometimes 0 (where you get correct output), sometimes it's not (garbage).

To fix this, use the %c format specifier to print a single character instead of a null-terminated string. If you really want to print strings, you need to make your string buffer larger and null-terminate it:

SYMBSIZE = 10, then have tabl[0].symbol[1] = '\0'; Note, you usually want to use string functions:

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

If your buffer isn't large enough for the string, it will overrun the buffer.

Your arrays aren't big enough to store strings in. You need an extra space for the null character. Change SYMBSIZE to 2 or print it as a char. Either one should work.

EDIT: Also, you're not putting the null chars in anyway. Printing a string will keep going until it hits \0, so you'd need to make SYMBSIZE = 2 and then make symbol[1] = '\0'; in order to print as a string.

EDIT2: Or just change your %s to a %c (print a single char at a time instead of the whole string)

To print out a char you should be using %c in the printf.

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

Your compiler should be warning you that the format string does not match the values passed. Compiler warnings are there for a reason.

If you want the chars, then you need to use %c and not %s to print the characters.

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

There is a way to solve the problem. Basically once we initialize the char array so we have to override it with garbage value to 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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top