Thread: clear a string

  1. #1
    Registered User
    Join Date
    Apr 2009

    clear a string

    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.

  2. #2
    Registered User
    Join Date
    Sep 2004
    mystring[0] = '\0';
    That will effectively clear your string. There is no need to fill it with null characters. But if you want to...
    memset(mystring, 0, mystringbuffersize);

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    There is no need to fill it with null characters. But if you want to...
    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.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I clear a string for later use?
    By scarlet00014 in forum C Programming
    Replies: 4
    Last Post: 10-15-2008, 11:57 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM