Is null character included while allocating using malloc

后端 未结 4 576
野趣味
野趣味 2020-12-03 12:49

I have been using C for quite sometime, and I have this trivial problem that I want to query about.

Say i want to create a character array that stores upto 1000 cha

4条回答
  •  萌比男神i
    2020-12-03 13:00

    You do indeed need to allocate the memory for the null terminator.

    Conceptually the null terminator is just a convenient way of marking the end of a string. The C standard library exploits this convention when modelling a string. For example, strlen computes the length of a string by examining the memory from the input location (probably a char*) until it reaches a null terminator; but the null terminator itself is not included in the length. But it's still part of the memory consumed by the string.

提交回复
热议问题