+ 1

How to return a string array from a function?

I am working on a program that : Extracts words from a line of input by the user to be used by other functions such as a function to check if items or words input by user are on the menue or string array in fuction or not My Program:- https://www.sololearn.com/compiler-playground/cgc4eCpji392/#cpp

25th Dec 2022, 1:09 PM
Gaurav Kaushik
2 Respuestas
+ 5
string* splitter( string input , int *n) { string *words; ... .. return words; } // Calling... string *words = splitter( input, n) ; https://code.sololearn.com/cmgis0kkSbm4/?ref=app
25th Dec 2022, 1:41 PM
Jayakrishna 🇮🇳
+ 3
You have completed the C++ course, so you probably know about the std::vector type. It is almost always better to return a vector of strings in such cases since returning pointers to memory allocated inside a function is risky (because you might forget to release the memory since it wasn't allocated manually by you (this is called a 'memory leak', a very common error by programmers)). In fact, std::vector should be preferred over manual heap allocation of arrays in almost all cases in C++.
25th Dec 2022, 9:10 PM
XXX
XXX - avatar