mixing pointers and arrays
Guy Harris
guy at rlgvax.UUCP
Sat Aug 13 16:23:26 AEST 1983
I agree that if you're going to treat structures as (mostly) first-class
citizens, you should treat arrays the same way. It would give you a
nice way to put "block moves" into code without having to use the USG UNIX
5.0 "memcpy"... routines as a side effect. (If you want to do this you
can always declare a structure whose only member is an array, but this
is a ghastly kludge).
One benefit of structure-valued arguments and returns is that if you
want to pass several arguments to a function that are *really* all part
of the same data structure, you can do it more cleanly with a structure-
valued argument. More importantly, it provides a way to get multiple
return values back from a function, which there is no other truly clean way
to do (you have to pass pointers to the return values otherwise). One
major nuisance with C (and 99% of all programming languages) is that a
routine can't return both a value and a status. The "read"/"write" system
calls, for example, must either return the number of bytes read/written
OR return an "abnormal termination" status - not both. So, you have the
problem of trying to have "write" on a magtape say "Well, I wrote all 5120
bytes, but I hit the EOT marker while doing it" or a "write" on a terminal
saying "Well, I got 100 of those bytes out before the guy hit the interrupt
key". The only way it could be done with only one return value is to have
something the convention that "errno" is cleared by all system calls unless
there is some abnormal condition; instead of testing for a return value of
"-1" (which is also a legitimate return value for some system calls) you
test for "errno != 0". An alternative would be to have the return value
from a particular system call declared as a structure containing the
returned value and the status code.
The disadvantage is that some present compilers aren't set up to do them
so they don't do them very well. PCC does some strange and non-reentrant
things for returning structure values, and the code in the compiler to
implement it is painful. Whether this is saying that the architecture of
C compilers should not now be the one used for PCC or that C should not
be extended to make aggregates first-class citizens is left as a question
for posterity to answer (as is the question of how UNIX system calls
should indicate abnormal status - note, not all abnormal statuses are really
errors, especially with I/O).
Guy Harris
{seismo,mcnc,we13,brl-bmd,allegra}!rlgvax!guy
More information about the Comp.lang.c
mailing list