How do I clear a string for later use?

This is a discussion on How do I clear a string for later use? within the C Programming forums, part of the General Programming Boards category; I have a string declaration like this: char *string; if string has chars in it, how do I clear it ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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
    21,739
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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