Why do strings in C need to be null terminated?

前端 未结 9 1185
青春惊慌失措
青春惊慌失措 2020-11-29 04:53

Just wondering why this is the case. I\'m eager to know more about low level languages, and I\'m only into the basics of C and this is already confusing me.

Do langu

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 05:19

    Because in C strings are just a sequence of characters accessed viua a pointer to the first character.

    There is no space in a pointer to store the length so you need some indication of where the end of the string is.

    In C it was decided that this would be indicated by a null character.

    In pascal, for example, the length of a string is recorded in the byte immediately preceding the pointer, hence why pascal strings have a maximum length of 255 characters.

提交回复
热议问题