which is correct:
orCode:#define SIZE 1024 ..... char buffer[SIZE]; ..... buffer[SIZE] = '\0'; .....
?Code:#define SIZE 1024 ..... char buffer[SIZE]; ..... buffer[SIZE-1] = '\0'; .....
which is correct:
orCode:#define SIZE 1024 ..... char buffer[SIZE]; ..... buffer[SIZE] = '\0'; .....
?Code:#define SIZE 1024 ..... char buffer[SIZE]; ..... buffer[SIZE-1] = '\0'; .....
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.
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
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
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.Originally Posted by KBriggs
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.Originally Posted by MK27
Last edited by laserlight; 06-05-2009 at 09:04 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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.Originally Posted by anon
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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