today we were talking about arrays
my teacher said that C only counts \0 as 1 byte - not 2 (as i thought),
is this true, or does he need to read up on C more often ?
today we were talking about arrays
my teacher said that C only counts \0 as 1 byte - not 2 (as i thought),
is this true, or does he need to read up on C more often ?
Last edited by rodrigorules; 09-08-2005 at 01:31 PM.
He is indeed correct the character '\0' is called the null termination character, (ASCII code 0) the \ means the next character in line (in this case 0) is an escape character type, not the actual number zero.
Null termination is used in character arrays to determine if the array is to be treated as a string or not.
This character array contains no null termination character, so C sees this as a simple array of characters, each its own entity(if you will...).Code:char str[] = {'H','e','l','l','o' };
This character array contains the same letters plus the extra null termination character, this to C is the string literal "Hello".Code:char str[] = {'H','e','l','l','o','\0' };
EDIT: Also note, that when loading a character array with a string you don't need to load it as i have done above
This will work jsut fine and C will add the null termination character for you.Code:char str[] = "Hello";
There are several escape characters just like this
there are several more, any standard C/C++ book will describe them to you.Code:\n = newline \t = tab \" = Quotes in a String Literal \' = Single Quote \\ = '\' character \b = backspace etc...
An important note on the null termination character -
You will notice that the array without the \0 in it is only 5 characters long, where as the array with the \0 is 6. This is important to note becuase if you are making a string you must make sure that it is big enough to house all of the information in the literal + 1 space for the null termination character, or you will get a buffer overflow error.
ERR this will cause an overflow error.Code:char str[5] = "Hello";
There we go, this will work, but remember you don't have that extra space to fill with anything, becuase the null termination character already took up that spot.Code:char str[6] = "Hello";
Listen to your teachers, they are right *most* of the time
Last edited by Charmy; 09-08-2005 at 01:45 PM.
That's not an overflow error, the null character is just discarded. In fact, this is a useful trick if you want an array of characters but not a string and you're not keen on using the verbose array initialization. If you want a string, it's easier to let the array size itself:You will notice that the array without the \0 in it is only 5 characters long, where as the array with the \0 is 6. This is important to note becuase if you are making a string you must make sure that it is big enough to house all of the information in the literal + 1 space for the null termination character, or you will get a buffer overflow error.
ERR this will cause an overflow error.Code:char str[5] = "Hello";
Code:char str[] = "Hello";Yea, way to make him doubt his teacher. How about "Listen to your teachers unless you have a good reason to doubt them".Listen to your teachers, they are right *most* of the time
Just because I don't care doesn't mean I don't understand.
He didn't say it was an overflow error, he said it would cause an overflow error which is certainly true the next time any string operation is performed on it. If you name a char array str, then it sure as hell better be null terminated.That's not an overflow error, the null character is just discarded
You obviously haven't been here long enough. If you doubt it just look at our resident instructorYea, way to make him doubt his teacher. How about "Listen to your teachers unless you have a good reason to doubt them".
The results is implementation dependent.. VC++ 6.0 produces this error and will not allow you to do that.Originally Posted by bithub
Dev-C++ does not produce and compile-time error or warning.Code:C:\dvlp\test1\test1.cpp(9) : error C2117: 'Hello' : array bounds overflow
Here is the output from the above, and the output is differentCode:#include <stdio.h> #include <stdlib.h> int main() { int i; char str[5] = "Hello"; int n = strlen(str); printf("len = %d\n",n); for(i = 0; i < n; i++) printf("%d ", str[i]); printf("%d\n"); system("pause"); return 0; }
each time I run the program. It cleary illustrates that str[] is not null terminated, as Narf said in an earlier post.
Code:len = 9 72 101 108 108 111 96 -3 127 25 25 Press any key to continue . . .
Last edited by Ancient Dragon; 09-08-2005 at 06:33 PM.
Ooh, semantics. By that argument, you can say that the other example will also cause an overflow error. Which is certainly true the next time you use an out of bounds subscript. Now, you can be willfully stupid and treat the array as if it's a string when you know it's not, and yes it will break, or you can use it as an array.He didn't say it was an overflow error, he said it would cause an overflow error which is certainly true the next time any string operation is performed on it.
Just because one teacher doesn't know C from a hole in the ground doesn't mean that all teachers are as dumb. If you don't trust your teacher, get another one. If you don't trust any teachers, don't you think you're a bit too cynical?You obviously haven't been here long enough.
Just because I don't care doesn't mean I don't understand.
The sad thing is that a high enough percentage of instructors don't really know the language that well. After my experiences in taking classes and seeing the posts of people here its pretty obvious. Rarely does one really have the ability to change instructors. Heck for my OOP C++, data structures, and discrete structures classes only one person taught that class. So if I wanted to change instructors, I'd have to go to another campus.ust because one teacher doesn't know C from a hole in the ground doesn't mean that all teachers are as dumb. If you don't trust your teacher, get another one. If you don't trust any teachers, don't you think you're a bit too cynical?
On a broader note, you should not automatically believe what your instructors say just because they are teaching it. Be critical, probe for reasons and answers. If nothing else it'll help you understand the material better.
There's a difference between healthy amount of skepticism and thinking all teachers are stupid. You can either try to learn the language as it's taught and correct bad habits later, or spend all of your time trying to prove your teacher wrong and learn nothing because you're trying to swallow everything all at once. That's my opinion, and I'm sticking to it.On a broader note, you should not automatically believe what your instructors say just because they are teaching it. Be critical, probe for reasons and answers. If nothing else it'll help you understand the material better.
Just because I don't care doesn't mean I don't understand.
That's not a string literal. It's simply an array of characters. You cannot modify string literals. Even this isn't a string literal:Originally Posted by Charmy
This is a string literal:Code:char foo[] = "not a string literal";
You can edit the individual characters in the first example, not in the second.Code:char *foo = "is a string literal";
Go read a Schildt book. It's not cynicism, it's healthy paranoia.Originally Posted by Narf
Quzah.
Last edited by quzah; 09-08-2005 at 07:30 PM.
Hope is the first step on the road to disappointment.
"String Literals are groups of characters surrounded by quotes". Neither version of foo in your examples are string literals -- the text within quotes are. In your first example, foo is a character array while the second example foo is a pointer.Originally Posted by quzah
Since you are posting on the C board, why don't you compile that as C, not as C++? You might get the same error, but I don't have VC, so I can't confirm.Originally Posted by Ancient Dragon
Last edited by swoopy; 09-08-2005 at 08:34 PM.
You're right -- I use *.c file and it compiled without errors or warnings. It produced similar test results that I got with Dev-C++ .Originally Posted by swoopy
Last edited by Ancient Dragon; 09-08-2005 at 08:46 PM.
>It produced similar test results that I got with Dev-C++ .
Which is what we expected. This is a case where it would be nice to get some warning from the compiler. Perhaps Dev-C++ may warn with the -Wall or -pedantic switch.
Where does this quote come from?Originally Posted by Ancient Dragon