I'm unable to make strncpy
when I want to copy 2 chars from string, but same code is working when I want to copy 3 chars.
In this example, I need strncpy()
to store 12 in 'to' variable:
void main(){
const char* from = "12345678";
char *to = (char*) malloc(3);
strncpy(to, from, 2);
printf("%s", to);
free(to);
}
but when I use strncpy(to, from, 3)
with malloc(4)
, it works ok. Any solution to make this work?