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

后端 未结 4 1235
一整个雨季
一整个雨季 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:10

    Imagine you're going to the airport

    You pick up a piece of paper and write down which terminal you need to go to, which flight and seat.

    You call for a taxi to the airport. The taxi company asks you "Which terminal". You consult your piece of paper and tell them "Terminal 3". On the journey, you tell your driver you are flying to Paris and he tells you "Oh, you really want to go to Terminal 1, it'll be much faster".

    You let him take you to Terminal 1. You make your flight, have a week in Paris and fly home. As you are unpacking, you find the original piece of paper.

    Does it say Terminal 1 or Terminal 3?

    This is an example of "pass by value" and in C and C++ pointers are actually variables much like any other. "Terminal 3" is a pointer, it is an address of a place at an airport. '0x073aff10' is a 32-bit integer value, but it could also be the address of the location in memory in which my credit card number is stored. If I tell you "Terminal 3" I am passing the address of the terminal I need by value.

    This line of code:

    void TestPoint(char* point)
    

    Declares a function called "TestPoint". Whenever this function is invoked, the computer will reserve new storage for a "char*" pointer and you will be able to refer to that storage in code with the variable name "point". The storage will be on the stack, when you leave the function, the stack "unwinds" upwards, the variable is gone.

    This line of code:

      TestPoint(test);
    

    Invokes the function test point, passing it the value of variable 'test'. What you see in TestPoint is not the same as 'test' its a copy of the value of test at the time you entered TestPoint.

    Pointers are basically variables, like any other, with a slightly special contract that lets you do some extra magic with them - you and the computer agree that they are going to contain memory locations of things.

    What you need is the address of the pointer itself:

    TestPoint(&test)
    

    and TestPoint needs to take a pointer-to-a-pointer-to-char

    void TestPoint(char** point)
    

    and in TestPoint you need to dereference "point" once to get to the pointer-to-char

    *point = new char[10]
    

    Alternatively, you could just make TestPoint take no argument and return a pointer value.

    char* TestPoint()
    {
        char point = new char[10];
        return point;
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-07 07:13
    void TestPoint(char** point)
    {
        *point = new char[10];
    }
    
    int main(int argc, char* argv[])
    {
        char* test = NULL;
        TestPoint(&test);
        /******/
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-07 07:14

    Try this, passing the address of the pointer,

    void TestPoint(char** point)
    {
        *point=new char[10]; //assign to location at point
    }
    int main(int argc, char* argv[])
    {
        char* test=NULL;
        TestPoint(&test);
        printf("test: %x\n",test);
        return 0;
    }
    

    A more C++ idiom would be to pass by reference,

    void TestPoint(char* &point)
    {
        point=new char[10]; //assign to point reference
    }
    int main(int argc, char* argv[])
    {
        char* test=NULL;
        TestPoint(test);
        printf("test: %x\n",test);
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题