Thread: How do I clear a string for later use?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    How do I clear a string for later use?

    I have a string declaration like this:

    char *string;


    if string has chars in it, how do I clear it back to NULL?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have no memory as you have declared it, so I'm not sure if you mean:
    Code:
    ...
       if (string == NULL)
           string = malloc(...);
        .... 
       free(string);
       string = NULL;
    or
    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.
    See also my other post in reply to one of your posts, regarding "wasing memory".

    --
    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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    string[0] = '\0';
    I hope you aren't just assigning a string literal to that pointer, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 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