top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Address of variable vs pointer

0 votes
47 views
old_fun()
{
  int a = 10;
  int *ptr = &a;
  new_fun(&a);  // passing address  case 1
  new_fun(ptr);  // passing address case 2 
}

new_fun(int *ptr)
{
  *ptr = 50; 
}

Here, case 1 will affect of "a" of old_fun but case 2 wont affect why ?

posted Nov 25, 2013 by sivanraj

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

2 Answers

+1 vote

Looks that you have missed something I tested the following code

new_fun(int *ptr)
{
  *ptr = 50; 
}

main()
{
  int a = 10;
  int *ptr = &a;
  new_fun(&a);
  printf("%d\n", a);
  new_fun(ptr);
  printf("%d\n", a);
}

Output

50
50
answer Nov 25, 2013 by Salil Agrawal
0 votes

I actually changed the new_func to this one in Salil's code

new_fun(int *ptr)
{
  (*ptr)++; 
}

and output was 11 12 so this question does not seems to be valid.

answer Nov 25, 2013 by Sanketi Garg
Similar Questions
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...