What is malloc doing in this code?

后端 未结 11 948
眼角桃花
眼角桃花 2021-01-01 04:59

Could you explain following code?

str = (char *) malloc (sizeof(char) * (num+1));
  1. What is malloc doing here?
11条回答
  •  萌比男神i
    2021-01-01 05:05

    Malloc allocates memory, in this case for the string str of length num. (char *) is the type for str sizeof(char) is the number of bytes required for each character. The +1 is for the trailing null character in the string, normally zero.

提交回复
热议问题