r/cprogramming • u/sudheerpaaniyur • Feb 11 '26
my mind is blasting because of this double pointer, please some explain me in better apaproach
Please explain why **ptr is used here, lets goodeep down.
#include <stdio.h>
#include <stdlib.h>
void modifyPointer(int **ptr) {//here i have Question
// Allocate memory for an integer
*ptr = (int *)malloc(sizeof(int));
if (*ptr == NULL) {
printf("Memory allocation failed\n");
return;
}
// Set the value at the allocated memory
**ptr = 42; // Dereference twice to set the value
}
int main() {
int *num = NULL; // Pointer to an integer, initially NULL
printf("Before modifyPointer: num = %p\n", (void *)num);
// Pass the address of the pointer (call by reference)
modifyPointer(&num);
printf("After modifyPointer: num = %p, value = %d\n", (void *)num, *num);
// Free the allocated memory
free(num);
return 0;
}
13
u/[deleted] Feb 11 '26 edited Feb 11 '26
A pointer is simply a reference. Imagine a postit-note on your door. It could say the information straight away. Or it says: "Hey look at the mirror, there is another postit note." And on the mirror, there might be another postit note saying: "Hey, look at the TV, I put another postit note there." Then you go to the TV and it says: "Hah, made you walk around the apartment."
That's pretty much it. A pointer is a number. By dereferencing it, you tell your computer, don't treat it as a normal number, treat it as an address and go to that address instead. There, you find a number, that you can now treat as an address again, going to your final location.
That can be infinitely deep.
But why would you use double pointer instead of pointing to the thing instead?
Well, that is because it allows you to change it in one place. Imagine, you have a list of things. You want to point to the first element. But that one keeps changing. So you create one pointer, that points at the head of the list. When you change the head, you update this one pointer. And your function is now saying, "don't just give me the head of the list, I don't trust that shit, give me a pointer to the pointer that points at the head of the list." That one stays always at the same place. You can safely give out references to all your functions. Even concurrently. It won't change.
EDIT: grammar, expression, clarity
7
u/EatingSolidBricks Feb 11 '26
What happens when you move out and all your stuff has your old address
Would it be nice if there was a place that had your address and you can change it there and it changes for all places that had your address
3
u/zhivago Feb 11 '26
Just remember that double pointers aren't a thing.
That's just a T * where T is int *.
It looks like you use it to fill in a int * in the caller.
e.g. int *p; foo(&p);
3
u/drbitboy Feb 11 '26 edited Feb 11 '26
Because in C, the arguments to all routines are always passed by value.
Say we start like this:
I hope it is obvious that
the line marked by (A) will print a value of -99,
because the value of num was initialized to -99
the line marked by (D) will print a value of 42,
because the value of num was assigned (overwritten) as -42
4
u/SmokeMuch7356 Feb 11 '26
It's because you're passing a pointer to
num:The variable
numhas typeint *; thus, the expression&numhas typeint **, so the corresponding parameter must also have typeint **.Remember that C passes all function arguments by value; when you call a function
each of the argument expressions is evaluated and the result of that evaluation is copied to the function's formal arguments:
ais a different object in memory fromx, so writing a new value toahas no effect onx.If we want
footo write a new value tox, we must pass a pointer tox:xandaare still different objects in memory, but instead of getting a copy ofx's value,agets it address. The expression*aacts as a kinda sorta alias forx.In general:
If we replace
Twith the pointer typeP *, we get this:The behavior is exactly the same; the only difference is the types. Since the variable
varhas typeP *, the expression&varhas typeP **.2
u/Brilliant-Orange9117 Feb 11 '26
The modifyPointer() allocates an memory on the heap, but instead of doing the normal and thing and returing the pointer to the allocated memory as return value it takes a pointer to a pointer as argument. Puts the address of the allocated memory into
*ptrand stores 42 in**ptr. It's just a contrived example to show what can be done. It shouldn't be done in such a simple case other than for more or less succesfully teaching new C programmers that pointers can point to other pointers.2
u/Kriemhilt Feb 11 '26
The correct approach to understanding indirection is to just draw boxes and arrows.
numis a box holding a pointer that points to NULL (a special value that isn't really its own box in any useful sense).The argument
&numis another box (that lives as long as the function call) which points tonum. It's the same box that is calledptrinside the function.2
u/agate_ Feb 12 '26
In C, all functions are pass-by-value, meaning the function cannot change any of its arguments. The solution is to pass in the address of the thing we want to change -- a pointer. So we don't change the address, but we use the address to change the thing.
But in this case, we want to change a variable containing an address -- we're changing a pointer. So we need to pass in the address of the pointer, which is to say a pointer to a pointer.
numis a pointer: a variable containing the memory address of an integer.In
main(), we setnumtoNULL, meaning it doesn't point anywhere.&numis the address ofnum-- a pointer to the pointernum.Inside
modifypointer(),ptris the address of a the address of anint.First, we want to set up memory for our
int, which means changing the pointer to theint. So we use *ptr to get the address of the int.Next, we want to change the value of the int, so we use
**ptr-- theintitself.1
u/umlcat Feb 13 '26
Looking for drawing representation of pointers, it will give you a better prespective ...
1
u/Big-Rub9545 Feb 13 '26
To modify a variable within a function (so that you don’t end up simply modifying a temporary copy of it), you pass its actual memory address to the function, in the form of a pointer.
But what if you want to modify the pointer itself? In this case, you want to change the memory address stored in the pointer variable itself so that it points somewhere else. Thus, you want to pass the actual memory address of the pointer to the function = a pointer to a pointer = a double pointer.
As usual, to modify the actual variable within the function you deference the pointer you passed to it (in this case: ptr). Since the actual variable here is itself a pointer, you can also alter the value it points to in the function by dereferencing it. That leads to dereferencing twice, hence the **ptr further down.
0
u/joejawor Feb 11 '26
*ptr is a pointer to a memory address. **ptr is the data located at that memory address
0
u/The_Ruined_Map Feb 11 '26 edited Feb 11 '26
As opposed to what? How would you propose implementing it?
It is completely unclear what specifically you are asking about. Double-pointer vs. singlr-pointer? Double-pointer vs. return value? Something else?
Your question makes zero sense until you explain what you'd offer as an alternative.