clear a string

This is a discussion on clear a string within the C Programming forums, part of the General Programming Boards category; how do I clear a string in C after I used it? I just want to clean it and fill ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    13

    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
    Location
    California
    Posts
    3,263
    Code:
    mystring[0] = '\0';
    That will effectively clear your string. There is no need to fill it with null characters. But if you want to...
    Code:
    memset(mystring, 0, mystringbuffersize);

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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
    Location
    Toronto, ON
    Posts
    3,545
    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, 12:57 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05: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, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21