Function does not change passed pointer C++

前端 未结 4 497
北海茫月
北海茫月 2020-11-22 01:58

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 ha

4条回答
  •  礼貌的吻别
    2020-11-22 02:30

    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];
    }
    

提交回复
热议问题