Pointer errors in the method of transmission(c++)

后端 未结 4 1319
一整个雨季
一整个雨季 2020-12-07 06:41

I encountered a problem.I try to change the pointer,but pointer does not change. Here is my code:

 void TestPoint(char* point)
{
point=new char[10];
}
int ma         


        
4条回答
  •  臣服心动
    2020-12-07 07:12

    That's because function TestPoint receives a copy of the test variable, modifies it (assigning pointer to allocated memory) and then discards. If you imagine replacing char* with, for instance, int, things may get clearer.

    The very minimal change you can do is rewrite TestPoint signature in the following way

    void TestPoint(char*& point)
    

    thus passing test variable via reference. These online lessons may be of help. Chapters 7.2 - 7.4 are about passing arguments to function.

提交回复
热议问题