top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Address of variable vs pointer

0 votes
368 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 LinkedIn 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
...