Assigning memory to double pointer?

后端 未结 7 881
广开言路
广开言路 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:39
     char **ptr;
        fp = fopen("file.txt","r");
        ptr = (char**)malloc(sizeof(char*)*50);
        for(int i=0; i<50; i++)
        {
           ptr[i] = (char*)malloc(sizeof(char)*50);
           fgets(ptr[i],50,fp);
        }
    
    fclose(fp);
    

    may be your typo mistake but your loop should be of 50 instead of 20 if you are looking for 50 x 50 matrix. Also after allocation of memory mentioned above you can access the buffer as ptr[i][j] i.e in the 2D format.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 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)   // <same as above>
    
    strcpy(str[0],"abcdefghij");   // 10 length character 
    strcpy(str[1],"xyzlmnopqr");   // 10 length character
    
    cout<<str[0]<<endl;    // to print the string in case of c++
    cout<<str[1]<<endl;    // to print the string in case of c++
    
    or
    
    printf("%s",str[0]);
    printf("%s",str[1]);  
    
    //finally most important thing, dont't forget to free the allocated mem
    free(str[0]);
    free(str[1]);
    free(str);
    
    0 讨论(0)
  • 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);

    0 讨论(0)
  • 2020-12-04 16:53

    Double pointer is, simply put, a pointer to a pointer, In many cases it is used as an array of other types.

    For example, if you want to create an array of strings you can simply do:

    char** stringArray = calloc(10, 40);
    

    this will create an array of size 10, each element will be a string of length 40.

    thus you can access this by stringArray[5] and get a string in the 6th position.

    this is one usage, the others are as mentioned above, a pointer to a pointer, and can be allocated simply by:

    char* str = (char*)malloc(40);
    char** pointerToPointer = &str //Get the address of the str pointer, valid only in the current closure.
    

    read more here: good array tutorial

    0 讨论(0)
  • 2020-12-04 16:59

    Adding to Pent's answer, as he correctly pointed out, you will not be able to use this double pointer once the function returns, because it will point to a memory location on the function's activation record on stack which is now obsolete (once the function has returned). If you want to use this double pointer after the function has returned, you may do this:

    char * realptr = (char *) malloc(1234);
    char ** ptr = (char **) malloc(sizeof(char *));
    *ptr = realptr;
    return ptr;
    

    The return type of the function must obviously be char ** for this.

    0 讨论(0)
提交回复
热议问题