throopw@dg_rtp.UUCP (Wayne Throop) (05/09/86)
>>Well. Sizeof is indeed peculiar. For example, >>sizeof(formal_array_name) yeilds your machine's pointer size. > > Ouch. First off (see that nit?), it took me two (admittedly > fraction-of-a-second) passes to understand that you were talking > about what sizeof yields [sp!] on an array declared as a formal > a r g u m e n t . Hmpf. I thought that the term "formal" used to mean "an argument to a function" was quite wide-spread in talking about algol-derived languages, and others besides. The value passed to a formal is normally called an "actual" or an "actual argument". Sorry about the confusion. I suppose I could have said "formal_argument_array_name". Using "formal_argument" rather than "argument" is, of course, intended to get around the ambiguity of whether I'm talking about something I'm passing to a function, or something that the function expects to be passed. Is there anybody (besides Joe) who thinks this abreviated usage ("formal" meaning "formal argument") is unclear? I'm not sure but what I'm tempted to continue using "formal" to refer to a formal argument, since it is so convenient. On the other hand, I don't want to confuse folks. > It's fairly clear that when you declare a formal > argument to a function, the C compiler coerces it from array-of-X > to pointer-to-X. You are therefore taking the size of a pointer, > and it returns the size of that pointer (which size may depend on > the type of the thing to which you are pointing). Yes, fine, wonderful, I agree, great. *BUT*. The formal was defined to be an array, by gar. It's sizeof should be array-like. Take this example, for example. void f(a,c,s,i) int a[10]; char c; short s; int i; { printf( "%d %d %d %d\n", sizeof a, sizeof c, sizeof s, sizeof i ); } void main(){ int a[10]; char c; short s; int i; f( a, c, s, i ); printf( "%d %d %d %d\n", sizeof a, sizeof c, sizeof s, sizeof i ); } This program produces the output 4 1 2 4 40 1 2 4 Now, by your theory, since "char" and "short" are promoted to int in the context of a call, it "ought to" have printed 4 4 4 4 40 1 2 4 Clearly, my way of having it print 40 1 2 4 40 1 2 4 is a smaller change to the language, and is thus preferable to yours. :-) More seriously, if one can say "f's formal is an array", the dratted thing should *behave* like an array, just like a char formal behaves like a char despite the actual being promoted to an int. Compilers do handstands now to see that char and short formals behave properly despite being transformed by the calling conventions. I think arrays should do the same. And before you say that "well, the coersion of array to pointer happens before the call", let me reply that that is irrelevant. The point is that the formal declaration is a white lie, and for some types this white lie is well hidden and for others it is not. I wish it to be equally well hidden for all types where this is possible. > As usual, this has been discussed to death in earlier articles. > If someone has a copy of my long-ish tutorial on this subject, > please send it to me. I promise not to re-post it more often than > every year or two, and flag it so you can junk it. Sadly, I don't have it. I remember it as being quite good, though. So it goes. > Joe Yao hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP} -- Wayne Throop <the-known-world>!mcnc!rti-sel!dg_rtp!throopw