Béla Karalyos Béla Karalyos - 13 days ago 5
C Question

I want to scan some words, and put them in a string array, then print it.

I want to scan some words, and put them in a string array, then print it. But all i got is some memory garbage...

char arrayOfWords[50][50];
char array2[50][50];

int i;
for (i=0; i<5; i++)
{
scanf ("%s" , arrayOfWords[i]);
array2[i][50] = arrayOfWords[i];

}

for (i=0; i<5; i++)
{
printf ("%s\n" , array2[i]);
}

return 0;

Answer

You can't just copy strings using the assignment operator (=).

Change:

array2[i][50] = arrayOfWords[i];

to:

strcpy(array2[i], arrayOfWords[i]);