Reputation: 748
I wanted to use double pointer. But, please tell me what I did wrong here. The value n does not update after the function called. I am expecting 30 but still see 10.
int main(int argc, char **argv) {
int n = 10;
int *ptr = &n;
int **ptr2ptr = &ptr;
function(&ptr2ptr);
printf("%d", n);
return 0;
}
void function(int *num) {
*num = 30;
}
Upvotes: 3
Views: 6934
Reputation: 95
void function(int ***num) {
***num = 30;
}
int main(int argc, char **argv) {
int n = 10;
int *ptr = &n;
int **ptr2ptr = &ptr;
function(&ptr2ptr);
printf("%d", n);
return 0;
}
What you did was give the address of double pointer, so it became triple pointer. Do the above it works fine.
Since code might get complex with triple pointer, just use a double pointer instead of sending address of double pointer:
void function(int **num) {
**num = 30;
}
int main(int argc, char **argv) {
int n = 10;
int *ptr = &n;
int **ptr2ptr = &ptr;
function(ptr2ptr);
printf("%d", n);
return 0;
}
Upvotes: 0
Reputation: 19221
You're both passing a triple pointer using &ptr2ptr
instead of *ptr2ptr
AND your function takes a pointer and not a pointer to a pointer (int *
instead of int **
).
Another way to look at it is that &ptr2ptr
takes the address of the pointer and it doesn't de-reference it. What you wanted was:
function(*ptr2ptr);
But to allow you to see more into the mistake, I took the longer route - you can look at this updated code and read through the comments for more info.
Remember, in C: "usage == declaration"
meaning that *
in a declaration creates a pointer and *
in usage dereferences (uses) a pointer.
&
will return the address of the object.
Here's some code to show the difference:
int main(int argc, char **argv) {
int n = 10;
int *ptr = &n; // this is a pointer to an int
int **ptr2ptr = &ptr; // this is a pointer to the pointer
// &ptr2ptr is a triple pointer - so we use the double pointer instead.
function(ptr2ptr);
// we could have used the original function using:
// function(*ptr2ptr);
printf("%d", n);
return 0;
}
// `int *num ` in your function prototype
// is referencing a pointer to a pointer,
// but it expects a double pointer - updated:
void function(int **num) {
// *num dereferences the first layer
// **num references the actual number.
**num = 30;
// or:
// *num = &30; // will fail to compile, unless 30 was a variable
}
Upvotes: 3
Reputation: 4454
Your function void function(int *num)
takes 1 argument,and that is a pointer to int
,so when you call it,you should supply it with the address of an integer,not with address of pointer to int
,nor address of address of pointer to int
.
int main(int argc, char **argv) {
int n = 10;
int *ptr = &n;
int **ptr2ptr = &ptr;
function(&n);
// function(ptr) /* this also works */
printf("%d", n);
return 0;
}
void function(int *num) {
*num = 30;
}
Upvotes: 2
Reputation: 106012
Since you are updating the value pointed by the pointer passed to the function, no need to pass pointer to pointer (you are passing actually pointer to pointer to pointer).
Function function
expects int *
type, therefore just pass ptr
. You have to change the function call
function(&ptr2ptr);
to
function(ptr);
Beside this you should have to include function prototype for function function
or define it before main
.
Upvotes: 2
Reputation: 83527
Let's look closely at each line of your code:
int main(int argc, char **argv) {
int n = 10;
n
is declared as an int
.
int *ptr = &n;
ptr
is declared as a pointer-to-int and assigned the address of n
.
int **ptr2ptr = &ptr;
ptr2ptr
is declared as a pointer-to-pointer-to-int and assigned the address of ptr
. This is what I assume you are calling a "double pointer".
function(&ptr2ptr);
Now you send the address of ptr2ptr
to function()
. Note that value you are attempting passed to function()
is a pointer-to-pointer-to-pointer-to-int (i.e. a "triple pointer").
printf("%d", n);
return 0;
}
void function(int *num) {
function()
is declared to take a pointer-to-int
. This means that the function call in main()
should cause a compiler error since the argument types do not match the parameter types.
*num = 30;
}
As I described above, your code should not compile. You are passing a triple pointer (int***
) to a function which accepts only a single pointer (int*
). You have at least 3 possible fixes:
Declare function()
to take a triple pointer:
void function(int*** num) {
***num = 30;
}
Declare function()
to take a double pointer:
void function(int **num) {
**num = 30;
}
and change the function call in main to
function(ptr2ptr);
Both of these seem completely unnecessary since you are only trying to change the value of a single int
. This means you can do all of what you want with a single pointer:
void function(int *num) {
*num = 30;
}
Now you can call this function with either
function(ptr);
or
function(&num);
Upvotes: 2
Reputation: 2537
Let me comment your code for you and explain what is going on:
int main(int argc, char **argv) {
int n = 10; //declaring variable n type int
//declaring variable ptr type int * setting it to address of n
int *ptr = &n;
//declaring variable ptr2ptr as int** and setting it to address of ptr
int **ptr2ptr = &ptr;
//now you are trying to take the address of ptr2ptr, which is the address of an int**
//this gives you an int***, you are passing this in function with undesirable behaviour
function(&ptr2ptr);
printf("%d", n);
return 0;
}
void function(int *num) //take in a parameter of type int*
{
*num = 30; //dereference it and put value 30 in it
}
Instead of calling function(&ptr2ptr)
you should call function(*ptr2ptr)
or even better function(ptr)
Upvotes: 2
Reputation: 696
You have to use the good input type for function : int**.
If you add some gcc flags like -pedantic, you will see that you made some mistakes on types.
A working version of your program is:
#include <stdio.h>
void function(int** num);
int main(int argc, char**argv) {
int n = 10;
int* ptr = &n;
int** ptr2ptr = &ptr;
function(ptr2ptr);
printf("%d",n);
return 0;
}
void function(int** num)
{
**num = 30;
}
Note that you don't need a double pointer to do what you want, a simple one is enough.
Upvotes: 2
Reputation: 144695
You are actually passing a triply indirected integer to function function
. &ptr2ptr
is the address of a pointer to a pointer to an integer. You did not define nor declare function
before calling it from main
. It is incorrect in C99, but supported in ANSI C and implicitly declares function
to take any number of arguments of any types and returning int
. You should move the definition of function
before main
and change it to:
void function(int ***num) {
***num = 30;
}
Upvotes: 6
Reputation: 433
#include <stdio.h>
void function(int** num)
{
**num = 30;
}
int main(int argc, char**argv)
{
int n = 10;
int* ptr = &n;
int** ptr2ptr = &ptr;
function(ptr2ptr);
printf("%d\n",n);
return 0;
}
Upvotes: 2