Thread: A question about string

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    A question about string

    If I have a string like this:

    char myString [5];

    But I didn't put any characters into the array
    does it mean:

    myString [0] = '\0'
    myString [1] = '\0'
    myString [2] = '\0'
    myString [3] = '\0'
    myString [4] = '\0'

    ????

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    No. It means you can't know for sure what it does contain until you initialize it to something.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by winggx View Post
    If I have a string like this:

    char myString [5];

    But I didn't put any characters into the array
    does it mean:

    myString [0] = '\0'
    myString [1] = '\0'
    myString [2] = '\0'
    myString [3] = '\0'
    myString [4] = '\0'

    ????
    no...
    your array contains garbage and so cannot be used as string till you initialize it...

    the simplest way to initialize string will be
    Code:
    char myString [5] = "";
    the alternative is


    Code:
    char myString [5] =  {0};
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    yep, vart said it right. but just a note:

    Code:
    myString [0] = '\0'
    myString [1] = '\0'
    myString [2] = '\0'
    myString [3] = '\0'
    myString [4] = '\0'
    inside of doing that, you could simple use the first line of code. since
    C stops processing a string after reaching the \0 sign.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    How about following code?
    Code:
     
    static char myString [5];
    Regards,
    Siddu

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Siddu_Kyocera
    How about following code?
    myString would have static storage duration, thus it would be zero initialised.
    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

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    As long as the string is defined globally, it's same as static and is initialized to zeroes. If it's defined inside a function then its memory is the stack. It is not zeroed out when the function is executed.
    Last edited by nonoob; 03-23-2010 at 04:19 PM.

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. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM