So i have a problem where the string in struct merge with another array. See the code and output for more explanation. Code:
#include <stdio.h>
#include <stdlib.h>
struct print
{
char code[3];
char name[10];
}test[2]={"001","Alen","101","Paul"};
int main()
{
int x;
for(x=0;x<2;x++)
{
printf("%s %s\n",test[x].code,test[x].name);
}
return 0;
}
Output:
001Alen Alen
101Paul Paul
Process returned 0 (0x0) execution time : 0.017 s
Press any key to continue.
The output is wrong, it should be like this:
001 Alen
101 Paul
So why the "name" merge in "code" variables? It shouldn't be like that. So how do i solve this? Thank you.
C strings need to be NUL terminated. Your code
array field is one character too small. Change char code[3];
to be char code[4];