Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!dino!atanasoff!hascall From: hascall@atanasoff.cs.iastate.edu (John Hascall) Newsgroups: comp.lang.c Subject: Re: best way to return (char *) Message-ID: <1137@atanasoff.cs.iastate.edu> Date: 23 Jun 89 19:15:19 GMT References: <7800013@gistdev> <894@cbnewsl.ATT.COM> Reply-To: hascall@atanasoff.cs.iastate.edu.UUCP (John Hascall) Organization: Iowa State Univ. Computation Center Lines: 29 In article <894@cbnewsl.ATT.COM> mpl@cbnewsl.ATT.COM (michael.p.lindner) writes: >In article <7800013@gistdev>, joe@gistdev.UUCP writes: >> Here is a question I haven't seen recently, and I'd like to get opinions from >> the collective wisdom of the group. 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? I have seen several approaches to this problem: >> . Have the routine allocate the buffer pointed to by the returned >> (char *) as a static. >AND >> . Assume it's the caller's problem to strcpy() (or other such) from the >> pointer before something else can use the space. >if they need to. I usually do this, where I can. Yuck. Routines which are not re-entrant are IMHO a "bad thing". Too often you want to do some thing like: foo( bar(3), bar(4) ); and if bar uses static storage for its result you are scr*wed. Unfortunately, the solution looks a bit untidy: for( bar(ptr1, 3), bar(ptr2, 4) ); John Hascall / ISU Comp Center / Ames, IA