I am writing a function that takes a string, string pointer and an int.
The function splits the string based on a set of rules and puts each token into an array. I need to return the array out of the function with the number of elements in the int variable etc. I am stuck as to how I return the array as I can not use auto other wise it is destroyed and I am reluctant to use new as I feel this is patchy.
I have other ideas on how to go about this but would like to see how other people go about this first. I could also be wrong and it could be possible to pass an auto out of an array. I can also not use vectors so there goes a copy constructor.
Vector can not be used as this was a challenge set out to me and I was asked not use to templates.
This is more of a C than a C++ question given those restrictions.
The common C pattern for returning an array is actually to get the caller to pass in an array to fill. This lets the caller decide on the allocation (and hence deallocation).
Your function prototype would look like
Where the function returns the number of elements written the pOutArray.
In the implementation you put in handling for pOutArray being NULL, in which case you just count the number of elements, and return that. This lets you call the function in one of several ways depending on your needs :-
or,