Assigning memory to double pointer?

2020-02-02 06:17发布

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("file.txt","r");
    ptr = (char**)malloc(sizeof(char*)*50);
    for(int i=0; i<20; i++)
    {
       ptr[i] = (char*)malloc(sizeof(char)*50);
       fgets(ptr[i],50,fp);
    }

instead of this I just assign a large block of memory and store the string

  char **ptr;
  ptr = (char**)malloc(sizeof(char)*50*50);

would that be wrong? And if so why is it?

7条回答
时光不老,我们不散
2楼-- · 2020-02-02 06:58
 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.

查看更多
太酷不给撩
3楼-- · 2020-02-02 07:01

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.

查看更多
太酷不给撩
4楼-- · 2020-02-02 07:04

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);

查看更多
做自己的国王
5楼-- · 2020-02-02 07:09

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);
查看更多
男人必须洒脱
6楼-- · 2020-02-02 07:12

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).

查看更多
迷人小祖宗
7楼-- · 2020-02-02 07:17

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

查看更多
登录 后发表回答