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

前端 未结 4 1082
走了就别回头了
走了就别回头了 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:24

    Its an undefined behaviour, you cannot say what can happen.

    Try on some other system you may get different output.

    The answer to your question is that it is an Undefined Behaviour and its output cannot be explained.

    In addition to above explanantion, in your particular case, you have declared array globally. Therefore in your second example a \0 is appended in the fourth byte of four-byte boundary as explained by Peter Raynham.

提交回复
热议问题