question about double pointer.

+1 vote
asked Aug 19, 2019 by Jongbo (460 points)
#include <stdio.h>
#include "malloc.h"
void main()
{
    short **pp;
    pp = (short**)malloc(sizeof(short*));
    *pp = (short*)malloc(sizeof(short));
    
    **pp=15;
    printf("**pp: %d",**pp);
    free(*pp);
    free(pp);
    
}

I was trying to understand this code. but i could not understand on one part.

pp = (short**)malloc(sizeof(short*)); //what does it mean by having two stars here (short**)

*pp = (short*)malloc(sizeof(short)); // and this part has only one star (short*)

i kind of know about because one is double pointer and one is just single pointer. but I couldnt understand fully about the number of pointer signs.

can someone help me please

3 Answers

–1 vote
answered Aug 20, 2019 by SWengineer
Try to think of the memory as a huge field of bytes. You can point into this field using a pointer. And of course you can store a pointer in this field. So you can point with a pointer to another pointer in this huge field.

An analog idea is derivation in mathematics. You derive a function and you can derive the derivative agin. Endlessly if you like.
0 votes
answered Nov 2, 2019 by Akhil Menon M (280 points)
pha panna nayinte mone

ninte appan answer parnaju therum
commented Nov 2, 2019 by Akhil Menon M (280 points)
i know only malayalam
this you convert malayalam to english
this the answer
0 votes
answered Nov 3, 2019 by gameforcer (2,950 points)

You can have pointers to values(variables):

    int x = 3;
    int* ptr = &x;
    printf("%d",*ptr);

 just as well as to the other pointers which is used for example in dynamically allocated 2D arrays:

    int rows = 3, cols = 5;
    int **arr = (int **)malloc(rows * sizeof(int *));

    for (int i=0; i<rows; i++)
         arr[i] = (int *)malloc(cols * sizeof(int));

 In the code above **arr is a pointer to a block of memory storing 'rows' ammount of pointers. Eeach of this pointers is arr[i] (which is by the way equal to '*(arr+i)' - if you've heard about pointer arithmetic then you'll probably know why it is like this) and means each row of the array. Then each of these rows is initialized  in 'for' loop similarly to initialization of **arr.

In your code it is similar so i'll try to explain it to you step by step:

    short **pp;
    pp = (short**)malloc(sizeof(short*));

is the same as:

    short **pp = (short**)malloc(sizeof(short*)); 

and what it does is it initializes pointer to pointer(s), in this case it is a pointer to a single pointer which is indicated by:

(sizeof(short*))

If it should point to, for example 4 pointers it would be:

(4*sizeof(short*))

 Next line initializes 1st and only row of this array so it stores 1 variable with the type of short.

So basically you get what would be a 2D array but has size of 1x1.

Next line after that is just giving this 1 place in array a value. By the way **pp is the same as pp[0][0].

Then it's just the matter of printing the variable out and cleaning memory after our 2D array.

I hope I helped, at least a little :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...