How to return as a pointer to a string?
How to return as a pointer to a string?
The comment says it all. Is there something you're having problems with?
Devoted my life to programming...
I think, "malloc in one function and free in other" is very bad thing. By the way, you needn't malloc at all. Just create a static array of constant strings and return appropriate string from array. What's problem?
Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev
Sometimes it's the best option available though.
You mean apart from the fact it's at odds with the requirements of the OP's particular homework exercise?
In general, use of statics imposes limits on ability to use a function. Such as non-reentrancy (and eliminating recursion), unsafe in multithreaded code, and it is often unsafe to use the function twice within one expression.
In this case it would be fine, were it not for the admonition to use malloc: the comment says "helper function for printRecord", so it is reasonable to suppose that the string will not be modified. Therefore, even with the use of a static local variable, the output of the function depends on the input, and not on any saved state that can vary from function call to function call.Originally Posted by grumpy
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)