Thread: erase all characters in a character array?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    44

    erase all characters in a character array?

    I have a character array of fixed length. How do I erase every character in this array? I can't use string.h or any other string manip class in my code.

    Code:
    for (int i = 0; i < fixedLenght; i++)
       charArray[i] = '?';
    What character do I set each char in the array to, to make it "empty"?


    Thanks,

    _d02

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you just want to make this char array an empty string then it is enough to set the first char to '\0'.
    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

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    To properly initialize an array, you will need to set each variable to either some defined variable such as a '1' or a '0' by a for loop, as used in most cases.

    or you can do for the first instantiation of the array

    Code:
       myarray[100] = {0};

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by slingerland3g View Post
    To properly initialize an array, you will need to set each variable to either some defined variable such as a '1' or a '0' by a for loop, as used in most cases.

    or you can do for the first instantiation of the array

    Code:
     
       myarray[100] = {0};
    That was a very subjective response.

    Code:
      myarray[100] = "";
    Could be good enough for most application.

  5. #5
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by diddy02 View Post
    I have a character array of fixed length. How do I erase every character in this array? I can't use string.h or any other string manip class in my code.

    Code:
    for (int i = 0; i < fixedLenght; i++)
       charArray[i] = '?';
    What character do I set each char in the array to, to make it "empty"?


    Thanks,

    _d02
    memset(charArray,'\0',fixedLength);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 05:48 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Selection Sort on 2-D character array
    By TankCDR in forum C++ Programming
    Replies: 6
    Last Post: 07-20-2003, 11:59 AM
  5. Character Array - almost? works
    By voltson4 in forum C Programming
    Replies: 3
    Last Post: 03-04-2003, 06:03 PM