I am fairly novice to c/c++ and I was trying to determine the best way to terminate a string when I ran across this.
I think I understand whats going on with incrementing a pointer and dereferencing it to a value of '\0'. The problem is, when I do it it doesn't work.
int main()
{
const char* c = "1234567890";
char* c1 = (char*) malloc(sizeof(char)*4);
strncpy( c1, c, 3 );
printf( "%s\n",c1 );
*c1++ = '\0';
printf( "%s\n",c1 );
}
The output is 123 23
It appears that the null value is being dropped at the beginning of the string as opposed to immediately after. Why?