Thread: terminating null

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    terminating null

    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.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    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.

    Code:
    char str[] = {'H','e','l','l','o' };
    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','\0' };
    This character array contains the same letters plus the extra null termination character, this to C is the string literal "Hello".

    EDIT: Also note, that when loading a character array with a string you don't need to load it as i have done above
    Code:
    char str[] = "Hello";
    This will work jsut fine and C will add the null termination character for you.

    There are several escape characters just like this
    Code:
    \n = newline
    \t = tab
    \" = Quotes in a String Literal
    \' = Single Quote
    \\ = '\' character
    \b = backspace
    etc...
    there are several more, any standard C/C++ book will describe them to you.

    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.
    Code:
    char str[5] = "Hello";
    ERR this will cause an overflow error.
    Code:
    char str[6] = "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.


    Listen to your teachers, they are right *most* of the time
    Last edited by Charmy; 09-08-2005 at 01:45 PM.

  3. #3
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    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.
    Code:
    char str[5] = "Hello";
    ERR this will cause an overflow error.
    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:
    Code:
    char str[] = "Hello";
    Listen to your teachers, they are right *most* of the time
    Yea, way to make him doubt his teacher. How about "Listen to your teachers unless you have a good reason to doubt them".
    Just because I don't care doesn't mean I don't understand.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That's not an overflow error, the null character is just discarded
    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.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yea, way to make him doubt his teacher. How about "Listen to your teachers unless you have a good reason to doubt them".
    You obviously haven't been here long enough. If you doubt it just look at our resident instructor

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by bithub
    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.
    The results is implementation dependent.. VC++ 6.0 produces this error and will not allow you to do that.
    Code:
    C:\dvlp\test1\test1.cpp(9) : error C2117: 'Hello' : array bounds overflow
    Dev-C++ does not produce and compile-time error or warning.
    Code:
    #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;
    }
    Here is the output from the above, and the output is different
    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.

  7. #7
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    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.
    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.
    You obviously haven't been here long enough.
    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?
    Just because I don't care doesn't mean I don't understand.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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?
    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.

    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.

  9. #9
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    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.
    Just because I don't care doesn't mean I don't understand.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Charmy
    Code:
    char str[] = {'H','e','l','l','o','\0' };
    This character array contains the same letters plus the extra null termination character, this to C is the string literal "Hello".
    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:
    Code:
    char foo[] = "not a string literal";
    This is a string literal:
    Code:
    char *foo = "is a string literal";
    You can edit the individual characters in the first example, not in the second.
    Quote Originally Posted by Narf
    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?
    Go read a Schildt book. It's not cynicism, it's healthy paranoia.


    Quzah.
    Last edited by quzah; 09-08-2005 at 07:30 PM.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by quzah
    Code:
    char foo[] = "not a string literal";
    This is a string literal:
    Code:
    char *foo = "is a string literal";
    You can edit the individual characters in the first example, not in the second.
    "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.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Ancient Dragon
    The results is implementation dependent.. VC++ 6.0 produces this error and will not allow you to do that.
    Code:
    C:\dvlp\test1\test1.cpp(9) : error C2117: 'Hello' : array bounds overflow
    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.
    Last edited by swoopy; 09-08-2005 at 08:34 PM.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by swoopy
    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.
    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++ .
    Last edited by Ancient Dragon; 09-08-2005 at 08:46 PM.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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.

  15. #15
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Ancient Dragon
    "String Literals are groups of characters surrounded by quotes".
    Where does this quote come from?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  5. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM