why can't I do :
strncpy (str2,NULL,5);
Printable View
why can't I do :
strncpy (str2,NULL,5);
Why? Because NULL is not a string.
Where is strncpy supposed to read the data (the source) to copy into the destination?
so what do I do to assign str2 to NULL?
If str2 is an array:
If str2 is a pointer:Code:str2[0] = '\0';
Code:str2 = NULL;
or if the string contains something sensitive like passwords, and you want to be extra secure, you can wipe the whole string like this:
Of course sizeof will only work if str2 is an array; if it's a pointer you need to know the actual size...Code:memset( str2, '\0', sizeof( str2 ) );
In most cases though, I'd just set the first character to NUL like Elysia showed.