#unable to grasp pointer to pointer

17 messages · Page 1 of 1 (latest)

sturdy turtle
#

Need help to understand pointer to pointer.i feel I don't have complete understanding of it

placid sealBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

pastel flint
#

It works exactly like a pointer to anything else, like a pointer to an integer.
But instead of getting an integer when you dereference it, you get another pointer.

#

Imagine C had a string datatype.
Then you surely would do, without any concerns:

string *strings = malloc(N * sizeof(string));
```to create a `pointer to string` called `strings` and allocate memory for `N` strings.

Now let's just replace that `string` with what we actually have in C: `char *` and the code suddenly looks like this:
```c
char **strings = malloc(N * sizeof(char *));
```Here you would also create a `pointer to string`, but now it's a `pointer to pointer to char`, and you would allocate memory for `N` `char pointer`s.
sturdy turtle
#

Why does passing the address of pointer resolved to ** in function and to access the said pointer you use single* in that function

pastel flint
#

and to access the said pointer you use single* in that function
I never access anything

#

!f

placid sealBOT
#
int* p;
get1024HeapMemory(&p);

int get1024HeapMemory(int** p) {
  *p = malloc(1024);
  if (*p == 0)
    return -1;  // error
  else
    return 0;  // success
}
sgr19
sturdy turtle
#

Like passing address of pointer causes it to show as a **p

pastel flint
#

malloc just returns you a void * (i.e. a pointer to void), which is special because a void * can be casted to any other pointer type and every pointer type can be casted to void *.

sturdy turtle
#

No no the malloc part. The get1024 function

pastel flint
#

Ah, you mean on the left hand side of the equals?

sturdy turtle
#

In main function when you pass the pointer address as argument it shows as **p and to access it shows *p how to visualise it

pastel flint
#

Okay, first of all you should use different names for the different variables in such code examples, as it makes it easier to argue about, so I will be talking about this code:

int get1024HeapMemory(int** heap_location) {
  *heap_location = malloc(1024);
  if (*p == 0)
    return -1;  // error
  else
    return 0;  // success
}

int main() {
    int *heap;
    get1024HeapMemory(&heap);
}

In that code you declare a pointer to int named heap which has not yet been assigned a memory address to point to.
You then pass the address of (i.e. a pointer to) heap to get1024HeapMemory, which means it assigns that value, i.e. the memory address of heap, to the heap_location variable. This means you pass a pointer to heap or a pointer to pointer to int, so the datatypes make sense.

You then dereference the heap_location to get basically the heap variable and then assign the 1024 bytes of newly allocated memory to that heap variable.

#

If you just substitute the values you get this:

int get1024HeapMemory(&heap) {
  *&heap = malloc(1024);
  if (*p == 0)
    return -1;  // error
  else
    return 0;  // success
}
```And note how `*&` always cancels itself out