I have a string declaration like this:
char *string;
if string has chars in it, how do I clear it back to NULL?
I have a string declaration like this:
char *string;
if string has chars in it, how do I clear it back to NULL?
You have no memory as you have declared it, so I'm not sure if you mean:
orCode:... if (string == NULL) string = malloc(...); .... free(string); string = NULL;
See also my other post in reply to one of your posts, regarding "wasing memory".Code:// memory has been allocated previously... string[0] = 0; // set the first char in string to zero - marks the end of the string at the first location.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I think you should clarify what you mean by clearing it. Matsp examples are right, but its tough to say which is what you are wanting considering the nature of the way you asked the question.
That declares a pointer to char named string. It could point to a lone char, or it could point to some char in a dynamically allocated array of char, or it could point to some char in another array of char that is on the stack.
As such, "clearing" the string is vague. Are you trying to correctly set the pointer to NULL? Are you trying to set the string that the pointer references to an empty string?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)