malloc memory to a pointer to pointer

后端 未结 4 1895
心在旅途
心在旅途 2021-01-24 01:02

I have came across this problem when using pointer to pointer to a char:

void setmemory(char** p, int num)
{
    *p=(char*)malloc(num);
}

    void test(void)
           


        
4条回答
  •  感动是毒
    2021-01-24 01:54

    You want to change the pointer, i.e., the function need a reference, not a value. In C, this is done by giving the address (pointer) to the variable that is by change a pointer itsself. Thus, one pointer is for referencing, the other for you porgram logic.

提交回复
热议问题