What is malloc doing in this code?

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

Could you explain following code?

str = (char *) malloc (sizeof(char) * (num+1));
  1. What is malloc doing here?
11条回答
  •  甜味超标
    2021-01-01 05:13

    malloc allocates an array of char (in this case) on the heap.

    array will be num+1 long, but the longest string it can possibly hold is 'num' long, because string in C need a ending null-byte.

提交回复
热议问题