best way to return (char *)
Chris Torek
chris at mimsy.UUCP
Fri Jun 23 14:57:06 AEST 1989
In article <7800013 at gistdev> joe at gistdev.UUCP writes:
>... Suppose I am writing a function that is going to construct a
>character string, and is going to return a pointer to that string.
>What is the best way to do this so that your pointer is sure
>to be valid when used?
What you are asking is not `How does one go about returning an object
of type pointer-to-char?', but rather `Where should one allocate space
for the characters?'. This question does not have a single best answer;
there is not sufficient information here to choose one. Most of the
approaches you listed are reasonable in some contexts, although if I
am right in my interpretation of this one:
> . Don't worry about it at all -- nothing is going to trash your memory
> at the pointed-to address before you can actually use it.
it is a bad idea. (My interpretation is that you mean something like
char *fn() {
char buf[SIZE]; /* but this is automatic storage */
...
return (buf);
}
This approach is particularly dangerous precisely because it often
works.)
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.lang.c
mailing list