52

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?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
c0ntrol
  • 908
  • 2
  • 9
  • 14

4 Answers4

101

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];
}
Andrew
  • 24,218
  • 13
  • 61
  • 90
  • 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);` – c0ntrol Aug 07 '12 at 09:28
  • @user1295618: what error do you see? Possibly i is out of range – Andrew Aug 07 '12 at 09:35
  • @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 – Andrew Aug 07 '12 at 09:42
  • @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 Aug 07 '12 at 09:51
  • @Andrew The first one can cause segmentation faults through pointer dereference? – Sohaib May 16 '17 at 02:03
  • @Sohaib If you pass invalid value - yes. But if *ptr is a valid address - there will be no problems – Andrew May 31 '17 at 08:41
  • Reference to the pointer actually is more comfortable to me, which aligns easy with the concept of reference to the `int *`. Reference to int is `int & i`, similarly reference to int* is `int* & i` – syam Sep 04 '21 at 20:55
32

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)
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • 12
    +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. – paxdiablo Aug 07 '12 at 09:02
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

AndersK
  • 35,813
  • 6
  • 60
  • 86
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++?

Community
  • 1
  • 1
mathematician1975
  • 21,161
  • 6
  • 59
  • 101