Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!utcsri!flaps From: flaps@utcsri.UUCP Newsgroups: comp.lang.c Subject: Style question re common C expression. Message-ID: <4934@utcsri.UUCP> Date: Mon, 15-Jun-87 01:18:39 EDT Article-I.D.: utcsri.4934 Posted: Mon Jun 15 01:18:39 1987 Date-Received: Tue, 16-Jun-87 00:38:40 EDT Reply-To: flaps@utcsri.UUCP (Alan J Rosenthal) Distribution: world Organization: University of Toronto Lines: 49 Frequently I write expressions of the form "x ? x : y". For example, in unix where "getenv" returns an environment variable, I might have something like: char *t; execl((t = getenv("EDITOR")) ? t : "/bin/ed",file,(char *)NULL); to run the user-specified editor, with a default of /bin/ed. My complaint about this syntax is mostly about having to use the temporary variable. I know assembler-level optimizers will elide it, but the point is that there is actually no operations happening conceptually between the '?' and the ':' and I don't think I should have to specify them. I think that "getenv("EDITOR") ? : "/bin/ed"" should do what I mean here. Unfortunately this is not supported; also, the expression "getenv("EDITOR") || "/bin/ed"" doesn't work because the value of the getenv is converted to 1 or 0. Also, in my current large project, for example, I have some functions which can accept pointers or NULLs for return values: for example, void f(ip) int *ip; { int trash; ... *(ip ? ip : &trash) = something; The same syntax applies here. (This can't be converted to an if statement because actually the pointer is passed to another function which dereferences it.) So the main question is, is there some nice way I missed to specify this? And the secondary question is, if not then is there a standard macro name and syntax I should use, like the standard "#define STREQ(s,t)(strcmp(s,t) == 0)"? -- // Alan J Rosenthal // \\ // flaps@csri.toronto.edu, {seismo!utai or utzoo}!utcsri!flaps, \// flaps@toronto on csnet, flaps at utorgpu on bitnet. "To be whole is to be part; true voyage is return."