Quick Null Termination Question

This is a discussion on Quick Null Termination Question within the C Programming forums, part of the General Programming Boards category; which is correct: Code: #define SIZE 1024 ..... char buffer[SIZE]; ..... buffer[SIZE] = '

Thread: Quick Null Termination Question

'; ..... or Code: #define SIZE 1024 ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    11

    Quick Null Termination Question

    which is correct:

    Code:
    #define SIZE 1024
    .....
    char buffer[SIZE];
    .....
    buffer[SIZE] = '\0';
    .....
    or

    Code:
    #define SIZE 1024
    .....
    char buffer[SIZE];
    .....
    buffer[SIZE-1] = '\0';
    .....
    ?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,041
    The second one.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    NUMBER TWO!

    Remember, the first element is zero. The first error is how we get an "off-by-one" error, closely related to the "fence post" error (believing that a 100' fence with posts spaced ten feet apart will require only ten posts).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    doesn't C append the '\0' to character strings automatically?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by KBriggs View Post
    doesn't C append the '\0' to character strings automatically?
    No. The string functions in string.h will. And the definition of a C string is a null terminated character array.

    But a character array such as "buffer" is not automatically a C string. If you are not using string.h functions to populate it, then you do need to add '\0' yourself.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by KBriggs
    doesn't C append the '\0' to character strings automatically?
    If you are talking about string literals, then yes, a null character will be appended in order for them to meet the definition of string in C. In this case we only have an array since we do not know what happens in the code that has been left out.

    Quote Originally Posted by MK27
    If you are not using string.h functions to populate it, then you do need to add '\0' yourself.
    However, not all the functions declared in <string.h> (even some of those whose names start with the letters "str") actually append a null character on each invocation of the given function.
    Last edited by laserlight; 06-05-2009 at 09:04 AM.
    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
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Neither is particularly correct since buffer doesn't contain anything but garbage.

    If you intend to use it as a C-style string and expect it to be empty then you should set the first char to 0: buffer[0] = '\0';
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by anon
    Neither is particularly correct since buffer doesn't contain anything but garbage.
    As a side effect of this nasty business of leaving things out of the code example... we don't actually know that either. Maybe buffer is a global variable and thus is 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

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    By the way . . . if you just want a reasonably large buffer, instead of #define'ing your own size, you can use BUFSIZ from <stdio.h>. Apparently this is an efficient size for your system, and I'm pretty sure it's always at least 512.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by tommyb05 View Post
    which is correct:

    Code:
    #define SIZE 1024
    .....
    char buffer[SIZE];
    .....
    buffer[SIZE] = '\0';
    .....
    or

    Code:
    #define SIZE 1024
    .....
    char buffer[SIZE];
    .....
    buffer[SIZE-1] = '\0';
    .....
    ?
    Second one is correct. Array indexing starts with 0 in C and ends upto size-1 so there is no element buffer[SIZE], and hence you can't do the first one. Although neither of them will give compilation error because in C, there is no check to see if the subscript used for an array exceeds the size of the array.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  3. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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