A question about string

This is a discussion on A question about string within the C Programming forums, part of the General Programming Boards category; If I have a string like this: char myString [5]; But I didn't put any characters into the array does ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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};
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,636
    Quote Originally Posted by Siddu_Kyocera
    How about following code?
    myString would have static storage duration, thus it would be zero initialised.
    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

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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, 05:44 PM

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