Pass a dynamic array of objects to function

前端 未结 3 2049
说谎
说谎 2021-01-06 16:58

I am in the process of learning c++. So i know a method by which you send something to function and then work as if it was call by value but actually it is call by reference

3条回答
  •  悲哀的现实
    2021-01-06 17:40

    Confusingly, a function parameter that looks like an array City p[] is actually a pointer, equivalent to City * p. So you can just pass a pointer to your dynamic array:

    list_cities(number, p);
    

    That said, prefer friendly types like std::vector so you don't have to juggle pointers and carefully avoid memory leaks and worse.

提交回复
热议问题