Thread: question about nul termination

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    question about nul termination

    for a char array define as below,

    char buff[6] = "hello";

    when cout << buff; the result is:
    hello

    right?

    when we debug that , we found that:
    buff[0] = h
    buff[1] = e
    buff[2] = l
    buff[3] = l
    buff[4] = o
    buff[5] = 0 (nul terminater)
    buff[6] = ? (unknown character)

    i would like to ask:
    1.) we can see "hello" after running the program but not "hello0?", is it because when the nul terminater reached, the progaram will stop printing the character?

    2.) for readfile example like below:

    char buff2[256]
    readfile ( handle, buff2, 256, &size,NULL)

    i.e. i read 256 characters to the array buff2 from the com port, will the program add the nul terminater to the char array??

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    char buff[6] = "hello";

    That statement makes an array with 6 characters. buff[6] is beyond the size of the array.

    Why'd you start a new thread for this?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    when a \0 null terminating character is reached cout stops printing the string and in your case the last element of the array of chars is ignored. You are correct.

    The length of the string is dictated by the amount of elements in the array before the \0 not the size of the array.

    The number of elements in the array -1 simply dictates the maximum length of string that may be stored.

    As for the second question, look at the docs for the function.
    Last edited by codec; 03-09-2003 at 09:42 PM.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    1) the null terminator has a value of 0 and is unprintable.Well you'll never see 1. '0' has an value of 40ish and is obviously different. In your example you do realise that buf[6] is not in the bounds of your array dont you.
    2) no readfile does not null terminate.plenty of info on api funcs at msdn
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    one thing that you should remember is that even though you have declared your string as buff[6] the string "hello\0" requires only 6 (0-5) elements so the array will be initialised as buff[5]

    daniel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM