besides passing an argument by reference (e.g. int &i), it is common to pass a pointer (int *p).
on the other hand, you may want to actually change the value of a pointer, for example a pointer to a string and want it to point to a different string. in that case you need to pass a pointer to the pointer (char **p) so that now instead of the string being "red", it is "green".
(of course that function could simple return a pointer to a different string (e.g. char *p = func(someArguments);)
I am trying to learn how we can change the value of pointer through function call.
We can pass the parameter by reference or by value. Pass by value means, the copy of the variable value is passed to the function.
The ptr variable in the code stores the memory location of the x variable. I want to create a function that when called will change the value of the ptr.
I think that the pointer tells where in memory the 'x' lives. If you change the pointer value, it will not find 'x' anymore, and will return some other value.
Yes. It is not clear what @kittu20 is after, or confused about.
Taken literally, this is what I came up with:
# include <stdio.h>
void foo(int **k)
{
(*k)++;
}
int main(void)
{
int x = 10;
int *ptr = &x;
printf("pointer is %p\n", ptr);
foo(&ptr);
printf("pointer is now %p\n", ptr);
return 0;
}
This was the output
pointer is 0x7fff4522668c
pointer is now 0x7fff45226690
...Program finished with exit code 0
Press ENTER to exit console.
HTH and beware of messing with pointers even when you do know what you are doing!
that's what i was looking. You pass a pointer to a pointer, i am trying to figure out why not pass only pointer
void foo(int *k)
{
(*k)++;
}
int main(void)
{
int x = 10;
int *ptr = &x;
printf("pointer is %p\n", ptr);
foo(&ptr);
printf("pointer is now %p\n", ptr);
return 0;
}
I am trying to figure out why compiler gives warning when passing pointer only
if you pass the pointer, it is the value of the point, for example, the address of x. if you change its value in the functoin, you are only changing the local argument's value (on the stack), not the value of the pointer from which the value taken and passed as an argument to the function.
int *foo(int *k)
{
return ++k;
}
int main(void)
{
int x = 10;
int *ptr = &x;
printf("pointer is %p\n", ptr);
ptr = foo(ptr);
printf("pointer is now %p\n", ptr);
return 0;
}
every variable is located at a specific location (address) in memory. a pointer is a variable with the value of an address.
foo (int x) is passed the value of "x". it is passed on the stack. changing x with foo() simply changes the value on the stack
foo(int *x) passes the address of "x" on the stack. the stack value is typically never modified. instead code with foo() will change the value of x from the calling code using the pointer, *p = 123. if "x" is located at address 0xFAB1000, that is the value passed to the function and used to change the value of "x".
pointer "p" could be set the the address of "x", int *p = &x;. if "x" is located at 0xFAB1000, that will be the value of p. but "p" may be located at 0xFAB20C0. if the the address of p is passed to foo(), foo (int **p), then its address, 0xFAB20C0 is passed to the function allowing the value of "P" to be modified in the scope of the calling function