-1

I have a code below :

#include <iostream>
#include <vector>
#include <string>
using namespace std;
void foo(int *a,int c){
    a=&c;
    cout<<"2-a:"<<*a<<endl;

}

int main()
{
    int *  a;
    int b=3;
    int c=6;
    a=&b;
    cout<<"1-a:"<<*a<<endl;
    foo(a,c);
    cout<<"3-a:"<<*a<<endl;
    return 0;
}

It gives me

1-a:3
2-a:6
3-a:3

Is there any way to write ?

1-a:3
2-a:6
3-a:6 
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
caesar
  • 2,865
  • 11
  • 29
  • 36

6 Answers6

4

You are passing in the pointer as value, i.e., the pointer is copied. If you want to be able to modify the pointer itself rather than the pointee, you'll need to pass in a reference:

void foo(int*& a,int& c){
    a=&c;
    std::cout << "2-a:" << *a << '\n';
}

Note that c is also passed by reference: otherwise a would be set to point to a temporary variable about to go out of scope. Any later access to a would be undefined behavior. Since a is declared in main() before c there is still a danger of triggering undefined behavior (if a is accessed after c goes out of scope; that isn't the case in your code, yet, but could cause grief later).

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
1

You need to pass the a and c variables as addresses or references to them. Example:

void foo(int **a,int &c){
    *a=&c;
    cout<<"2-a:"<<**a<<endl;

}

foo(&a, c);

Passing c as reference is necessary too, as the address of it must be valid even outside the function. If you declared it as void foo(int **a, int c), the c variable would be valid only in the function body and a would then point to invalid memory.

See answers to this SO question

Community
  • 1
  • 1
Erbureth
  • 3,378
  • 22
  • 39
0

You should never change parameters – they represent inputs to a function. The proper way is to return the output value(s):

int foo(int c) {
    cout << "2-a:" << c << endl;
    return c;
}

This is good for several reasons, among them that it makes interfaces uniform and limits unpredictable side-effects (this in turn makes functions easier to reason about and thus less error-prone).

Apart from that, Dietmar’s answer shows how to modify parameters. But, again: the correct way of solving this is by returning the value, not by modifying arguments.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
0

Explanation of the foo function:

The foo function takes a pointer to an integer value and an integer value. Those variables are stored on the stack.

void foo(int *a, int c) {

The following statement means: a should now points to the address of variable c. This does not change the original pointed value.

    a=&c;

So, *a and c are now sharing the same value because a points to c.

    cout<<"2-a:"<<*a<<endl;

As a consequence, foo takes 2 arguments and always print the second.

}

Modification of the function:

You actually want the foo function to put the content of c into the cell pointed by a. So you should write:

void foo(int *a, int c) {
    *a = c;
    cout<<"2-a:"<<*a<<endl;
}

Test

 % g++ code.cpp
 % ./a.out 
1-a:3
2-a:6
3-a:6
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
-1

change function to as follows.

void foo(int *a,int * c){
    a=c;
    cout<<"2-a:"<<*a<<endl;
}

and call it with foo(a,&c);

Dhaval
  • 1,046
  • 6
  • 10
-1

you can try

void foo(int **a,int c){
    (**a)=c;
    cout<<"2-a:"<<**a<<endl;

}
Moamen
  • 11
  • 2