Thursday, September 20, 2007

pointer to const memory


char * s = "ab";
memset(s, 0 , strlen(s)); //empy string

This code won't run because s is considered pointer to const char.
As well, this applies on a statement like this:

memcpy(s, "\0\0" , strlen(s));

That's because the compiler first reserved a memory space to hold the value "ab" then it returns to you a pointer to it. i.e. this is not considered intialization of the pointer.
Of course, defining s as an array of characters will be ok, where s will be handled as a normal variable that can be inialized not as pointer to const memory.

No comments:

Post a Comment