0

This is my code, after a = b; in the function, a is still nullptr....

int getBox(int *a) {
  int *b = new int;
  *b = 3;
  a = b;
  std::cout << *a;
}

int main() {
  int *a = nullptr;
  getBox(a);
  std::cout << a;
}

I guess it's a very simple problem... Maybe I forgot too much about C++

7
  • 1
    You have a=&b twice. Can you be more specific about where this occurs? Commented Jul 6, 2020 at 15:05
  • If you want to update the value of it in the method, I'd use a reference (not a pointer). You're working with stack memory atm, and b is undefined afaik after getBox returns Commented Jul 6, 2020 at 15:06
  • You assigned address of local variable of a function to a pointer. Commented Jul 6, 2020 at 15:07
  • 1
    You passed a pointer by value if you want to update the pointer in the callee pass it ref. Commented Jul 6, 2020 at 15:08
  • 1
    @Bobi.Liu -- There are only two ways to pass items in C++. 1) By value, 2) By reference. A pointer is a value, thus all the "rules" that apply to pass-by-value applies to pointers. Commented Jul 6, 2020 at 15:16

5 Answers 5

2

I'm not sure what you're trying to do, but this row inside the getBox():

a=&b;

Doesn't actually change a in the main, you actually overrides the pointer(the copy that was made by the function), and make it point somewhere else.

You can do something like this(again, I don't see the point) :

int getBox(int ** a){
    int *b = new int;
    *b=3;
    *a=b;
    std::cout<<*a;
}

int main(){
    int *a= nullptr;
    getBox(&a);
    std::cout<<a;

}
Sign up to request clarification or add additional context in comments.

Comments

1

Let's assume there is some type T. Now here are 3 different kinds of functions:

void f(T a) {  // pass by value (this is a copy of the 'a' in main)
  a = /* something else */ ;
}

int main() {
  T a = /* something */ ;
  f(a);
  // a is still something
}
void f(T &a) {  // pass by reference (this is a reference to the 'a' in main)
  a = /* something else */ ;
}

int main() {
  T a = /* something */ ;
  f(a);
  // a is now something else
}
void f(T *a) {  // pass by address (this is a pointer to the address the 'a' in main)
  *a = /* something else */ ;
}

int main() {
  T a = /* something */ ;
  f(&a);
  // a is now something else
}

Now you can apply this logic to any T you want, such as int, or int*, and the same rules will work. You can try this out with getBox and see the effect of each version, which should help you understand what's going on. Note that you are using the first version (pass by value), but for the result you are expecting, you should use the second version (pass by reference).

Comments

1

If you really want to change what a is pointing to, then you can think it this way maybe it will help to make it a bit easier to understand. A is an int pointer and the function getBox takes a reference that you can modify its value which is an int pointer.

void getBox(int* &a) {
    int *b = new int;
    *b = 3;
    a = b;
    std::cout << *a;
}

int main(){
    int *a= nullptr;
    getBox(a);
    std::cout<< *a;
}

This will change the value of a, which is a new pointer value to b.

Comments

0

Yes of course, why should changing a in getBox change the value of a in main? If you think the answer is 'because it's a pointer' then I'm afraid you've misunderstood pointers.

Look at this code

int getBox(int a){
    a=3;
    std::cout<<a;
}

int main(){
    int a= 0;
    getBox(a);
    std::cout<<a;

}

Setting a=3 in getBox has no effect on a in main. Your code is exactly the same, but for some reason because pointers are involved beginners often think it works differently. It doesn't.

You can however use pointers in this way to change what is being pointed at, that's the important thing, but changing the pointer itself doesn't work in the way you are expecting.

1 Comment

Perhaps you can phrase this in a less condescending, and more useful, way.
0

You probably only want to change to getBox(int * & a). You then pass a reference to the pointer a to the function instead of creating a copy of the pointer that points to the same address in your case NULL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.