0

Heloo GUYS ..I am passing an alias to pointer in function and in that function i am assigning pointer to another variable .This is changing the address of the pointer passed in main funtion .i'll show u example of my code .This gives the output 40.

#include <iostream>
using namespace std;
void foo(int* &p){
    int z=40;
    p=&z;
}
int main(){
    int x=10;
    int *p=&x;  
    foo(p);
    cout<<*p;
}

But when I try to do this all in one main function then the address of the the pointer doesnt change so as the ouput..this is my code

#include<iostream>
using namespace std;
int main()
    {
       int a=1, b=2, c=3;
       int *p, *q;
       p=&a;
       q=*&p;
       p=&c;
       cout<<*q<<endl;

    } 

it gives the ouput 1 rather then the 3.THANKs..

6 Answers6

1
void foo(int* &p){
    int z=40;
    p=&z;
}

Here pis a reference to a pointer. Which means that when you modify it you will modify the referred pointer too.

When you call foo with

int main(){
    int x=10;
    int *p=&x;  
    foo(p);
    cout<<*p;
}

You're saying that the reference named pin the function foo refers to the pointer named pin the function mainso whatever you do to p in foowill affect p in main.

#include<iostream>
using namespace std;
int main()
    {
       int a=1, b=2, c=3;
       int *p, *q;
       p=&a;
       q=*&p;
       p=&c;
       cout<<*q<<endl;

    } 

Here you have no reference anywhere, you just have a pointer that you copy to another pointer. q=*&pis equivalent to q=p.

The key difference to note here is that the & operator in the first exemple means "reference" while the & operator in the second example means "address of".

If you want the closest equivalent to the first sample with the second one, try this:

#include<iostream>
using namespace std;
int main()
{
  int a=1, b=2, c=3;
  int *p, *&q = p;
  p=&a;
  q=*&p;
  p=&c;
  cout<<*q<<endl;

}

And in this last case you have both an & which means "reference" in int *p, *&q = p; and an & that means "address of" in q=*&p;

Drax
  • 12,682
  • 7
  • 45
  • 85
0
void foo(int* &p){
    int z=40;
    p=&z;
}

The above code is not safe because whoever called foo now has a pointer to a destroyed stack variable.

it gives the ouput 1 rather then the 3.THANKs..

Of course it does. Learn what a pointer is and how it works.

int a=1, b=2, c=3, *p, **q;
p=&a;
q=&p;
p=&c;
std::cout << **q << std::endl;
inetknght
  • 4,300
  • 1
  • 26
  • 52
0

just thought you should know that in foo() you're assigning the pointer to the address of a variable with automatic storage duration, which means that what it points to when foo returns is actually undefined (and in a larger program this is a disaster waiting to happen).

In your second example you only assign q once. During an assignment, a reference is the object it's referring to. The reference dependency is not carried forward.

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
0

That's because you aren't changing q. You are changing p. When you do

q=*&p;

it is the same as

q = p;

So what you have done is assigned p to point to a. Then you assign the value of p to q so now q points to a. Then you assign the address of c to p. Doing that does nothing to q and as such q still points to a.

You should also read Can a local variable's memory be accessed outside its scope? to see why your first example has undefined behavior.

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

Why do you expect to get the answer as 3?

enter image description here

q points to p then you changed where p points. p points to c but q will still point to where previous address of p.

unless you reassign q to point to p, it will still point to a.

sam
  • 2,033
  • 2
  • 10
  • 13
0

Let me only consider your second example and I added a few comments and hopefully it will clarify why you are getting that output.

   int a=1, b=2, c=3;
   int *p, *q;
   p=&a; //P now points to the variable a
   q=*&p; // q now points to the value in pointer p
   p=&c;
   cout<<*q<<endl; //You are printing  value from pointer q

Please note you are not changing the address of a pointer, instead you are changing only the address where pointer points to.

Steephen
  • 14,645
  • 7
  • 40
  • 47