::: Area #157 (comp.sys.acorn.programmer) Message: #34788 (Read 4 times, has 0 replies, 1488 bytes) Date : Thu Aug 6 14:29:52 1998 From : Phil Norman of fidonet#2:254/27.2 To : All Subject: Re: Help Needed with getenv (C) On Wed, 5 Aug 1998, Stewart Brodie wrote: > char *Strdup(const char *s1) > { > if (s1 == NULL) { > return NULL; > } > else { > const size_t length = strlen(s1) + 1; > char *const s2 = malloc(length); > > if (s2 == NULL) return NULL; > return memcpy(s2, s1, length); > } > } Nah, you don't wanna do that, you wanna do this: strcpy((newstring=(char *)malloc(1+strlen(source)))? newstring : (exit(1), 0), source); Note the interesting use of (exit(1), 0). The above line won't compile without this evil appendage because the return type of exit(1) is void, and you can't typecase a void to anything else, and the return type of the ?: thingy cannot be void. Fun eh? Enjoy, Phil P.S. I'd like to point out, before I get flamed to death, that I would never, NEVER use this in any code. Other than the one time, but that was just to check if it really did work (it did). -- Rule of programming #1: Everything that can go wrong will go wrong, except when you're running the debugger. Phil Norman, mailing from Oregan Networks. email: forrey@eh.org URL: http://newton.ex.ac.uk/general/ug/norman --- EchoMaker 0.48 * Origin: The Arcade BBS Usenet News Gateway (2:254/27.2)