I need to fill manually an array of chars. I declared it like this:
char* coor = malloc(sizeof(char) * 5);
Then I manually assigned every variable to its position:
coor[0] = O_colum;
coor[2] = ((char)(O_row+48));
coor[3] = '-';
coor[4] = D_colum;
coor[5] = ((char)(D_row+48));
(D_Row
and O_row
are integers, I need that number in character form, not the equivalent value in ASCII; that’s why I do +48
)
The problem comes when I try to print it. If I use printf(" %s", coor)
it only prints the first characters and I don’t know why. I’m using %s
, so it should print all the characters in the string.
When I do this:
char *p = "hello";
printf("%s",p);
It does print hello
.
sizeof(char)
will never differ from1
as that is defined by the standard. So nothing gained using it. If you want to always use the correct type, usesizeof(*coor)
. – too honest for this site Jul 5 '15 at 22:32