erase all characters in a character array?

This is a discussion on erase all characters in a character array? within the C Programming forums, part of the General Programming Boards category; I have a character array of fixed length. How do I erase every character in this array? I can't use ...

  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
    22,272
    If you just want to make this char array an empty string then it is enough to set the first char to '\0'.
    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

  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, 08:25 PM
  2. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 06:48 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Selection Sort on 2-D character array
    By TankCDR in forum C++ Programming
    Replies: 6
    Last Post: 07-20-2003, 12:59 PM
  5. Character Array - almost? works
    By voltson4 in forum C Programming
    Replies: 3
    Last Post: 03-04-2003, 06:03 PM

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