Storing a string in an array of chars without the null character

前端 未结 4 1084
走了就别回头了
走了就别回头了 2021-01-14 10:07

I\'m reading the C++ Primer Plus by Stephen Prata. He gives this example:

char dog[8] = { \'b\', \'e\', \'a\', \'u\', \'x\', \' \', \'I\', \'I\'}; // not a s         


        
4条回答
  •  野的像风
    2021-01-14 10:28

    The '\0' is just a solution to tell how long is a string. Lets say you know how long it is by storing a value before the string.

    But your case is when you intentionally leave it out the functions and normally your code as well will keep searching for the delimiter ( which is a null character ). It is undefined what is behind the bounds of a specified memory it greatly varies. In Mingw in debug mode with gdb its usually zeroed out, without gdb its just junk... altho this is just my experience. For the locally declared variables they are usually on the stack so what you are reading, is probably your call stack.

提交回复
热议问题