0

Please have a look at code below :

#include <stdio.h>   
void main()
{
    int a=30;
    int *var=&a;
    Change(&var);
    printf("%d %d",*var,a);
}

int Change(int**ptr)
{
    **ptr=40;
}

#include <stdio.h>
void main()
{
    int a=30;
    int *var=&a;
    Change(var);
    printf("%d %d",*var,a);
}

int Change( int *ptr)
{
    *ptr=40;
}

The output from both programs is 40 40

We are passing the copy of address of var so why it is reflected in actual main function and producing output 40 40?

Why both of these programs are producing same output?

David C. Rankin
  • 69,681
  • 6
  • 44
  • 72
  • 1
    Please format your code – Andrew Li Jul 06 '16 at 07:07
  • Which one of the two programs you don't understand? – Jabberwocky Jul 06 '16 at 07:17
  • Both of them @MichaelWalz – SHUBHAM TANDAN Jul 06 '16 at 07:20
  • See [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/) — though this is tangential to the issue being discussed in the question. – Jonathan Leffler Jul 06 '16 at 07:21
  • Actually i'm having problem in understanding the double pointer used in the first program .what is the main difference between these two program ? – SHUBHAM TANDAN Jul 06 '16 at 07:25
  • `&var` passes the address of `var` and Change uses double pointer because, `var` is pointing to address of `a`. So, Derefrencing single pointer on `&var` (address of var) will point to the value stored in `var` i.e., `a`'s address, so again derefrencing it, will point to the value 30. – Abhineet Jul 06 '16 at 07:29
  • 1
    What output did you expect, and why? – M.M Jul 06 '16 at 07:29
  • The more interesting case is where you dynamically allocate storage for `var` in `Change`. Then the pointer/double-pointer distinction, and why?, becomes instantly clear. – David C. Rankin Jul 06 '16 at 07:34
  • take a look here: [http://stackoverflow.com/questions/5580761/why-use-double-pointer-or-why-use-pointers-to-pointers] – Tommylee2k Jul 06 '16 at 07:43

4 Answers4

2

Both programs are equivalent. In first one, you passing pointer to pointer (i.e. address of pointer variable), and then dereferencing it twice. In second one, you are are just passing pointer (i.e. address of actual variable) and dereferencing it once.

Sameer Naik
  • 1,088
  • 12
  • 25
  • 1
    they are equivalent. But: in the first, you could change *ptr to point to a different int, and get a printf like " 30 40 ", which is not possible in the second program, where the 2 values printf'ed will always be the same, since in Change() you always work on a – Tommylee2k Jul 06 '16 at 07:47
2

It seems to me you need the following illustration to get the answer on your q:

Pointers Diagram

AnatolyS
  • 4,111
  • 14
  • 27
1

The output is the same because you write 40 via the same pointer to int. In the second case you pass pointer-to-int, which you successfully dereference with * operator and obtain lvalue with type int, to which you then write 40. In the first case you pass pointer-to-pointer-to-int. And then you do double dereferencing: **ptr or the same as *(*ptr). Inner * is applied first and makes the expression with type pointer-to-int inside braces, and that pointer-to-int is the same as was passed to Change() in the second case. And at the end - outer * works just like for the second case.

Sergio
  • 7,657
  • 2
  • 22
  • 45
1

&var passes the address of var and Change uses double pointer because, var is pointing to address of a. So, Derefrencing single pointer on &var (address of var) will point to the value stored in var i.e., a's address, so again derefrencing it, will point to the value of a i.e., 30.

For single pointer Change, I think, you should figure that out now from the above details.

Abhineet
  • 5,031
  • 22
  • 41