Addresses of parameters

Karl Heuer karl at haddock.ima.isc.com
Fri Oct 28 06:21:45 AEST 1988


In article <35664 at XAIT.Xerox.COM> g-rh at XAIT.Xerox.COM (Richard Harter) writes:
>In article <10124 at haddock.ima.isc.com> karl at haddock.ima.isc.com (Karl Heuer) writes:
>>I've never considered it unportable to initialize auto variables to an
>>arbitrary expression.
>
>Well, I didn't say it was not portable -- I said that I wouldn't think of
>using it.

I was responding to the previous poster, who considered it a "non-portable
abbreviation for an extra line of code".  I agree, btw, that there are things
to be avoided in this area as a matter of style (I won't list them, lest I
start a new flame war), but I don't take it to the extreme that you do.

>>Similarly, I feel justified in using expression of type "char *" even though
>>there exist [word-based] architectures ...
>
>The cases are not similar.  Expressions of type "char *" are fundamental to
>the language.  Taking the address of a calling sequence parameter is an
>oddity ...

I think it's merely a difference in degree.  If someone tried to market an
alleged C compiler that couldn't handle "char *", they'd be laughed out of
existence.  If they get sloppy on parameter addressing, they can probably
shrug it off as a "minor bug" for a while.

>Are you sure, by the way, that such a guarantee is in the major language
>specifications?  Taking an address is not invariably guaranteed.

Yes, I'm sure.  It's implicit in K&R (which is not, and was not intended to
be, a precise language specification).  According to the dpANS (the formal
spec), the only lvalues that cannot be &'d are registers and bitfields.
(There also exist lvalues that are not modifiable, namely arrays and consts;
this is an orthogonal concept.)

>>I've declared it as a variable; the declaration appears as part of the
>>function header.  It should be just as addressible as any other variable
>>I've declared.
>
>{register int i; thing = &i;}

My statement stands.  Non-parameter variables are addressible if and only if
they are not declared register; the same holds for parameters.  This makes it
"just as addressible".

>[The point is] that taking the address of a parameter is not a useful kind of
>thing to do.

I'll grant that it's not all that common, but I think it can be as useful as
taking the address of an auto variable, and for the same reasons.  In effect,
a parameter *is* an auto variable which is initialized to the value of the
actual argument.

Your reasoning raises a couple of other questions.  Do you ever change the
value of a parameter variable, or do you treat it as a constant?  Do you ever
declare a formal parameter with the "register" keyword?

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list