how do I clear a string in C after I used it? I just want to clean it and fill it up with null characters.
how do I clear a string in C after I used it? I just want to clean it and fill it up with null characters.
That will effectively clear your string. There is no need to fill it with null characters. But if you want to...Code:mystring[0] = '\0';
Code:memset(mystring, 0, mystringbuffersize);
Just in case you don't know why you would want to, virtually all code (including the standard library) that works with strings considers the null character the end of the string, regardless of how much space is allocated. If for any reason, this is not the case for you (very unlikely), then that won't work.There is no need to fill it with null characters. But if you want to...
One reason why you might want to set all the chars to '\0' is if the string contains sensitive security info like a password and you don't want to risk it being found in a core dump, buffer attack or bug in the program. But in all other cases, I'd just set the first character to '\0' instead.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010