What is the point of pointers?

后端 未结 12 1317
梦谈多话
梦谈多话 2021-02-01 18:49

What is the point of pointers in C++ when I can just declare variables? When is it appropriate to use them?

12条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 19:38

    Sometimes you have a function that needs to return some amount of memory that isn't a set amount, such as reading from a file.

    bool ReadDataFromFile(char** contents);
    

    You would declare a char* contents and pass the address of that pointer to the function. That function would then allocate the memory and your pointer would point to the contents upon return.

提交回复
热议问题