macro to specify output parameter

Chen Tu ht01 at bunny.UUCP
Fri Aug 15 05:01:51 AEST 1986


    When I read C functions, many times I was confused about the roles
    played by their parameters:  it is hard to tell a pointer
    parameter is an array or a pointer to a scaler (to be used as an
    output parameter).  To distinguish an output parameter from an
    array parameter, where both use the '*' notation, I found it is
    useful to have a macro to replace the notation of the former.

	    #define OUT(name) *name
	     
	    /*** output parameter: ***/
	    foo(c)
	    char OUT(c);   <== output parameter, point to a char.
	    {       ...
		    OUT(c) = 'a';
		    ...
		    }
	     
	    /*** array parameter: ***/
	    foo(s)
	    char *s;      <== input parameter, a character string.
	    {
		    ... *s++ ... }
	     
    To make the code more readable, do not use the output parameters
    in other places, and only use OUT(name) on the lhs of assignments, i.e.,
    treat them as OUTPUT ONLY parameters.

    When the output parameter is also a pointer, we have
	    foo(s) char * OUT(s); { ...; OUT(s) = "this is a test"; ...}
    compare to
	    foo(s) char **s; { ...; *s = "this is a test"; ...}
    The former seems more readable and understandable.

    Another alternative style is: (I have no preference)

	    #define OUT *
	    foo(c) char OUT c; { ...; OUT c = 'a'; ...}

				Hai-Chen Tu
				GTE Laboratories
				Waltham, Mass 02254
				(617) 466-4124
				CSNET: ht01%gte-labs.csnet



More information about the Comp.lang.c mailing list