54

I have my function and I am filling targetBubble there, but it is not filled after calling this function, but I know it was filled in this function because I have there output code.

bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
    targetBubble = bubbles[i];
}

And I am passing the pointer like this

Bubble * targetBubble = NULL;
clickOnBubble(mousePos, bubbles, targetBubble);

Why it is not working?

4 Answers 4

103

Because you are passing a copy of pointer. To change the pointer you need something like this:

void foo(int **ptr) //pointer to pointer
{
    *ptr = new int[10]; //just for example, use RAII in a real world
}

or

void bar(int *& ptr) //reference to pointer (a bit confusing look)
{
    ptr = new int[10];
}
Sign up to request clarification or add additional context in comments.

7 Comments

But when I try your approch it crash on this line *targetBubble = bubbles[i]; and I am passing argument like this clickOnBubble(mousePos, bubbles, &targetBubble);
@user1295618: what error do you see? Possibly i is out of range
@user1295618: please ask a new question and post your new code there. Because it's hard to say what's the problem without seeing your actual code
@user1295618: they are the same and should work both. Possibly you have undefined behavior in your program and it work differently from launch to launch
@Andrew The first one can cause segmentation faults through pointer dereference?
|
33

You are passing the pointer by value.

Pass a reference to the pointer if you want it updated.

bool clickOnBubble(sf::Vector2i& mousePos, std::vector<Bubble *> bubbles, Bubble *& t)

1 Comment

+1. The first answer that gets it right in the context of C++. Answers suggesting doubly-indirected pointers are the old C way of doing this.
27

if you write

int b = 0;
foo(b);

int foo(int a)
{
  a = 1;
}

you do not change 'b' because a is a copy of b

if you want to change b you would need to pass the address of b

int b = 0;
foo(&b);

int foo(int *a)
{
  *a = 1;
}

same goes for pointers:

int* b = 0;
foo(b);

int foo(int* a)
{
  a = malloc(10);  // here you are just changing 
                   // what the copy of b is pointing to, 
                   // not what b is pointing to
}

so to change where b points to pass the address:

int* b = 0;
foo(&b);

int foo(int** a)
{
  *a = 1;  // here you changing what b is pointing to
}

hth

Comments

8

You cannot change the pointer unless you pass it by (non const) reference or as a double pointer. Passing by value makes a copy of the object and any changes to the object are made to the copy, not the object. You can change the object that the pointer points to, but not the pointer itself if you pass by value.

Have a read of this question to help understand the differences in more detail When to pass by reference and when to pass by pointer in C++?

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.