char array null terminator
I don't know who invented it, but at least in C/C++ when you have a string contained in an array of char you won't be able to tell where the string ends, you could argue that you know the array length but the string could be shorter, so the idea of the '\0' character is to tell where that happens in the array.
example:
char s[10]:
s[0]='\\';
s[1]='0';
s[2]='\0';
s[3]='x'; // won't be printed
printf("%s",s); // will print: \0
example:
char s[10]:
s[0]='\\';
s[1]='0';
s[2]='\0';
s[3]='x'; // won't be printed
printf("%s",s); // will print: \0
Quote:Original post by jcullet
Joel SpolskyQuote: you should avoid ASCIZ strings like the plague
Is this C++ char?
Quote:Original post by CalinIs this C++ char?
A C++ char array.
In general you'll find that people says that arrays are evil, cause they don't check you're writting in they space an many other reasons, they are telling you to use a higher-level type such as std::string instead of char[].
I was wandering if that's what he's talking about.
Quote:they don't check you're writting in they spacerephrase please
Quote:Original post by Calin
I was wandering if that's what he's talking about.Quote:they don't check you're writting in they spacerephrase please
Some compilers lets you go with this:
char s[10];
s[12] = 'x';
where you're writting beyond the space of memory assigned to the array, that can cause the program to crash or corrupt some data, maybe nothing happens, that depends on what was previously on that space.
EDIT: for further information about why array are 'evil' check this page:
http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement