Assigning memory to double pointer?

后端 未结 7 918
广开言路
广开言路 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:53

    other simpler way to memorize

    Case -1 :

    step-1 : char *p;

    step -2 : please read it like below

    char (*p); ==> p is a pointer to a char

    now you just need to do malloc for the type (step-2) without braces

    i.e., p = malloc(sizeof(char) * some_len);

    Case -2 :

    step-1 : char **p;

    step -2 :

    please read it like below

    char* (* p); ==> p is a pointer to a char *

    now you just need to do malloc for the type (step-2) without braces

    i.e., p = malloc(sizeof(char *) * some_len);

    Case -3 :

    No one uses this but just for sake of explanation

    char ***p;

    read it as,

    char** (*p); ==> p is a pointer to a char** (and for this check case-2 above)

    p = malloc(sizeof(char**) * some_len);

提交回复
热议问题