[comp.lang.c] Returning error codes from a function

JI8@psuvm.psu.edu (Dave Jeffery) (10/31/90)

  We would like to write a function that returns a pointer to void
 (void *). In this function there are several conditions that can
 occur that would be considered an error. Rather than simply
 returning NULL we would like to return more detailed error information.
 Several ideas have been discussed but one that seems to have the most
 promise is to define several global void pointers and return them.
  Please note that this is in a multi-tasking environment where a global
 error code is not acceptable, and the overhead of passing additional
 parameters is too high.

   void *error1 = "error1";
   void *error2 = "error2";

   void *ourfun(int p1);

   main() {
    ...
    if (ourfun(10) == error1) ...
    ... }

   void *ourfun(int p1) {
    ....
    return error1;
    ...  }

  Our concerns are:
   1. Does this violate ANSI C ?
   2. Is it portable ?

  In your replies, please site where in the ANSI C standard, or any
 other publication, this issue is clarified.

----
 Dave Jeffery           JI8@psuvm.psu.edu

gwyn@smoke.brl.mil (Doug Gwyn) (10/31/90)

In article <90303.114803JI8@psuvm.psu.edu> JI8@psuvm.psu.edu (Dave Jeffery) writes:
>   1. Does this violate ANSI C ?

No, it's okay.  The most important reference is 3.3.16.1.

>   2. Is it portable ?

No, because pre-ANSI compilers generally did not support void*.
Since these error coded are really char* anyway, why not just
use char*.  In fact a typedef would be nice:
	typedef char *ErrorCode;
	typedef const char ErrorDef[];	/* for unchanging constants */
	#define NO_ERROR 0	/* or some other form of null pointer */

	static ErrorDef	error1 = "error1";
	...

	ErrorCode ourfun(args) {
		...
		return error1;
	}
There are all sorts of possible variations on this theme.