char array null terminator

Started by
6 comments, last by Calin 15 years ago
Who's the inventor/what's the meaning? [Edited by - Calin on April 22, 2009 3:34:47 AM]

My project`s facebook page is “DreamLand Page”

Advertisement
Joel Spolsky has a pretty nice and relatively famous article on c-style strings.
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
Quote:Original post by jcullet
Joel Spolsky
Quote: you should avoid ASCIZ strings like the plague


Is this C++ char?

My project`s facebook page is “DreamLand Page”

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 space
rephrase please

My project`s facebook page is “DreamLand Page”

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 space
rephrase 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
read it, guess I run into the rear end of the bunny.

My project`s facebook page is “DreamLand Page”

This topic is closed to new replies.

Advertisement