Assigning memory to double pointer?

后端 未结 7 914
广开言路
广开言路 2020-12-04 16:18

I am having trouble understanding how to assign memory to a double pointer. I want to read an array of strings and store it.

    char **ptr;
    fp = fopen(\         


        
7条回答
  •  佛祖请我去吃肉
    2020-12-04 16:46

    A double pointer is just a pointer to another pointer. So you can allocate it like this:

    char *realptr=(char*)malloc(1234);
    char **ptr=&realptr;
    

    You have to keep in mind where your pointer is stored at (in this example the double pointer points to a pointer variable on the stack so it's invalid after the function returns).

提交回复
热议问题