Assigning memory to double pointer?

后端 未结 7 911
广开言路
广开言路 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条回答
  •  -上瘾入骨i
    2020-12-04 16:49

    i will give one example, which might clear of the doubt,

    char **str;                              // here its kind a equivalent to char *argv[]
    str = (char **)malloc(sizeof(char *)*2)  // here 2 indicates 2 (char*)
    str[0]=(char *)malloc(sizeof(char)*10)   // here 10 indicates 10 (char)
    str[1]=(char *)malloc(sizeof(char)*10)   // 
    
    strcpy(str[0],"abcdefghij");   // 10 length character 
    strcpy(str[1],"xyzlmnopqr");   // 10 length character
    
    cout<

提交回复
热议问题