Terminating Zero

This is a discussion on Terminating Zero within the C Programming forums, part of the General Programming Boards category; I have a question about termination zero '

Thread: Terminating Zero

' in an array of characters. Lets say I create: char array[10]; My ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    10

    Terminating Zero

    I have a question about termination zero '\0' in an array of characters. Lets say I create:

    char array[10];

    My Question is: Is the will the terminating zero be at array[9]? Or is there memory for 10 chars, index 0-9?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    The array would not have been initialised, so there may not even be a null character in the array. There will be enough memory for 10 characters, which means that the array can be used to store a null terminated string of up to a length of 9.
    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
    Join Date
    Jun 2009
    Posts
    10
    Right, should of given example with an initialized char array. Assuming then that the array is full of characters, the terminating zero will be at the last index and only 9 non '\0' chars will be stored. Is this correct?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by DickArmy
    Assuming then that the array is full of characters, the terminating zero will be at the last index and only 9 non '\0' chars will be stored. Is this correct?
    Yes, but more accurately, assuming that the array contains the longest possible string that it can contain.
    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

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by DickArmy View Post
    Right, should of given example with an initialized char array. Assuming then that the array is full of characters, the terminating zero will be at the last index and only 9 non '\0' chars will be stored. Is this correct?
    That depends entirely on what initializer you give. If you give for instance "hello", you'll get 'h', 'e', 'l', 'l', 'o', '\0', '\0', '\0', '\0', '\0'. If you give an initializer that's too long, the first ten characters should be placed in the array, and what happens to the rest ....

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    char array[10] = "123456789";
    will be terminated by a zero.

    Code:
    char array[10] = "123456789A";
    Will NOT be terminated, as it fills the 10 characters of the array, and there is no space for a terminating zero.

    If the initializing value is shorter than 9 characters, the terminating zero will be before the [9] position, but the remainder of the space in the array will be zero as well.

    --
    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.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    10
    thanks for the help... <3

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    @dickarmy
    Initializing more elements than the size of the array is an error except for char where it's not. Although C allows it but C++ throws an error.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by tabstop View Post
    If you give for instance "hello", you'll get 'h', 'e', 'l', 'l', 'o', '\0', '\0', '\0', '\0', '\0'.
    Are you sure that is the standard? I'm not sure, but I can't find anything saying whether it will be filled with \0's or just be 'h', 'e', 'l', 'l', 'o', '\0', ????
    I always thought it was the latter, but I'm not sure. A quick gcc tests fills the array with \0's, but I'm not sure if that is just them being nice or standard.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by linuxdude View Post
    Are you sure that is the standard? I'm not sure, but I can't find anything saying whether it will be filled with \0's or just be 'h', 'e', 'l', 'l', 'o', '\0', ????
    I always thought it was the latter, but I'm not sure. A quick gcc tests fills the array with \0's, but I'm not sure if that is just them being nice or standard.
    The standard states that if you use an initializer that is short (contains fewer elements than the array), the remainder of the array will be zero-initialized. (Think
    Code:
    int bob[50] = {0}
    .)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. error: missing terminating " character
    By nasim751 in forum C Programming
    Replies: 2
    Last Post: 04-17-2008, 01:50 AM
  3. terminating a while loop with a character
    By just_learning in forum C Programming
    Replies: 3
    Last Post: 04-13-2007, 05:22 AM
  4. terminating wrong process
    By Queatrix in forum Windows Programming
    Replies: 12
    Last Post: 09-09-2006, 01:16 AM
  5. terminating input
    By Chaplin27 in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2004, 05:32 PM

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