macro to specify output parameter
Guy Harris
guy at sun.uucp
Sat Aug 16 08:58:37 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).
Well, actually, pointer parameters are never arrays; arrays and pointers are
two completely different things. This may be a good argument for allowing
"&" to be applied to arrays (yielding a pointer value of type "pointer to
array of <whatever>", NOT type "pointer to <whatever>"!) and using "pointer
to array" as the argument type if you really want to pass a pointer to the
array.
Of course, in the second example routine "foo", it may not really be a
pointer to the first element of an array/string; that routine might be used
to stuff characters into some arbitrary place in a string, so it's not
really a pointer to a string/array, it's just a pointer to a "char" that is
assumed to be somewhere within a string/array.
This may, in fact, also be an argument for reference types a la C++; there,
your routine "foo" would be written as
foo(c)
char &c;
{
...
c = 'a'; /* store 'a' in the "char" referred to by
"c" */
...
}
This is how call-by-reference is done in C++; in other languages that
implement call-by-reference, output parameters are generally passed by
reference.
Your second proposed style comes close to implementing this as best as can
be done in C. Try
#define REF * /* for defining reference types */
#define DEREF * /* dereferencing operator; since C doesn't
know about reference types, it won't
automatically dereference them */
foo(c)
char REF c;
{
...
DEREF c = 'a';
...
}
--
Guy Harris
{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
guy at sun.com (or guy at sun.arpa)
More information about the Comp.lang.c
mailing list