Archived

This topic is now archived and is closed to further replies.

ryanc

What char. represents null?

Recommended Posts

ryanc    136
Does ''\n'' represent the null or does ''\0'' represent null. Plus: When you declare a statement like: char name[21]; ,does name have a possibility of 21 characters or is it 20 plus 1 reserved for the null? Does the counting start from 0-21 or 1-21? Tips are appreciated! Thanks! RyanC (C++ Beginner) --------------------------- Games are only as good as you make them...

Share this post


Link to post
Share on other sites
Quantum    122
when you declare
char name[21]
you have 21 different elements, ranging from name[0] to name[20]
the \0 representing the end of the string is automatic, you dont have to worry about it

Share this post


Link to post
Share on other sites
daveb    122
Whoa whoa whoa, that is not true at all.

If you have :

char name[21];

One character in that array _must_ be a ''\0'' otherwise its not a properly terminated string. At that point you''re basically counting on whatever is next in memory (which is code and linker dependant) to contain a zero at some point.

The longest properly terminated string you can have with an array of 21 characters is a 20 character string plus the one character NULL terminator.

Share this post


Link to post
Share on other sites
Snyper    122
Just remember that just because you have an array of chars, doesn't make it a string, unless you append the null char at the end.

Snyper

Edited by - Snyper on October 30, 2000 8:47:32 PM

Share this post


Link to post
Share on other sites
Quantum    122
quote:
Original post by daveb

Whoa whoa whoa, that is not true at all.

If you have :

char name[21];

One character in that array _must_ be a ''\0'' otherwise its not a properly terminated string. At that point you''re basically counting on whatever is next in memory (which is code and linker dependant) to contain a zero at some point.



hmm.. i didn''t know that.
so in char name[21] you cannot use the 20th element..
e.g.
char[0] = ''a'';
..
char[20] = ''x''; //this has to be \0?

Share this post


Link to post
Share on other sites
TrIaD    122
...it depends what you use it for...

if you''re just using it in your own program, and you KNOW that it''s 21 characters, it doesn''t neccessarily HAVE to be null-terminated...

but if you pass it to anything that''s expecting a variable-length string, you MUST put a ''\0'' in there somewhere so it knows where the end is...

remember, all you''re passing is an address to the first character... the function has no way to know how long it is (well, some you tell them explicitly, but most you don''t), so you need that char... otherwise it keeps reading until it finds it and thinks your string it "Hello World!yyyyyyyyyyyyy" (only it''s a y with an umlout (spelled wrong, I know) over it, that''s I think 0xCC or something close), or the remainder of memory for some other program... I''ve seen email addresses floating in my memory before...

A little side-note: you can also say char[20]=0; rather than char[20]=''\0''; ...use the ascii code instead of the actual ascii character...

e.g. these mean the same
char[2]=''0'';
char[2]=48;

--Tr][aD--

Share this post


Link to post
Share on other sites