6

Please find the code snippet as shown below:

#include <stdio.h> 

int My_func(int **); 

int main() 
{ 
     int a =5;
     int *p = &a;
     My_Func(&p);
     printf("The val of *p is %d\n",*p);
}

void My_Func(int **p)
{
     int val = 100;
     int *Ptr = &val;
     *p = Ptr;
}

How does using a double pointer as an argument in my_Func function and also making a change of value, both reflect the same in the main function? If we use a single pointer in My_Func, why does that not change the value in main? Please explain this for me with examples if possible.

3
  • 4
    As a side note, don't return pointers to local variables. When you've returned from a function, its local variables are not valid anymore. Commented Jun 9, 2013 at 8:52
  • Please explain what you're trying to accomplish. Commented Jun 9, 2013 at 8:53
  • @meaning-matters: I just wanted to know why arguments as double pointers reflect the value in main when changed in the My_func definition? Commented Jun 9, 2013 at 8:57

4 Answers 4

8

int **p is a pointer to a pointer-to-int. My_Func(int **p) works by changing the value of integer that the pointer-to-int points to i.e. int a.

Without changing the implementation, the function will not work with a pointer-to-int parameter int *p as there is a second level of indirection. In addition, you're setting the value to a local variable that is created on the stack. When the function is completed the memory used for the variable will be reclaimed, therefore making the value of a invalid.

void My_Func(int **p)
{
     int val = 100; // Local variable.
     int *Ptr = &val; // This isn't needed.
     *p = Ptr;
} // val dissapears.

Remove the second level of indirection and copy val by value instead of pointing to it:

#include <stdio.h>

void My_Func(int *p) 
{
    int val = 100;
    *p = val;
}

int main(void) 
{
    int a = 5;
    My_Func(&a);
    printf("The val of a is %d\n", a);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

7

In short, in C when you pass something as a parameter, a copy will be passed to the function. Changing the copy doesn't affect the original value.

However, if the value is a pointer, what it points to can be changed. In this case, if you want to affect the pointer, you need to pass a pointer to it down to the function.

Comments

0

Use it in the function declaration:

void func(int *p)
{
  int val =100;
  int *temp=&val;
  p=temp;
 }

p starts pointing to another address i.e. address of val. So it will print the value 100.

Important note: Try it in your downloaded compiler (always in case of pointers) not in the online compiler. The online compiler doesn´t keep track of lost addresses in stack.

Comments

0

You are assigning the address of local variable, which will soon disappear when My_Func returns. You can use following in your code. However you can do the same thing just by using single pointer, double pointer is not required in this example.

void My_Func(int **p)
{
     int val = 100;
     int *Ptr = &val;
     **p = *Ptr;
}

1 Comment

I am late here, hope you don't mind I ask: I have the exact problem, I tried passing both single and double pointers and change the value within a function, in both cases, the original values did get changed. My understanding is that we need to use a double pointer to change the original value within a function. Hope you could shred some light on this. Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.