[comp.lang.c] parameter adjustment

rbutterworth@watmath.waterloo.edu (Ray Butterworth) (02/15/88)

In article <2292@umd5.umd.edu>, chris@trantor.umd.edu (Chris Torek) writes:
> Now then, on to parameters:
> There are no array parameters in C.  While you can declare
> a parameter as `array <N> of <T>', or even (belying what I
> said above) as `array of <T>', the parameter's type is NOT
> `array <N> of <T>'.  It is quietly adjusted by the compiler
> to `pointer to <T>'.
> NONE OF THE ABOVE APPLIES TO `ordinary' VARIABLE DECLARATIONS---
> ONLY PARAMETER ARRAY DECLARATIONS ARE `adjusted'.

C preforms this "helpful" adjustment of the programmer's
misdeclaration with other parameters too.
Consider this program (BSD 4.3):

% cat sizeof.c
#include <stdio.h>

test(f, d, s, c, a)
    float f;
    double d;
    short s;
    char c;
    int a[10];
{
    auto float af;
    auto double ad;
    auto short as;
    auto char ac;
    auto int aa[10];

    fprintf(stdout,
        "sizeof args:   float=%d  double=%d  short=%d  char=%d  int[10]=%d\n",
        (int)sizeof(f), (int)sizeof(d), (int)sizeof(s),
        (int)sizeof(c), (int)sizeof(a));
    fprintf(stdout,
        "sizeof autos:  float=%d  double=%d  short=%d  char=%d  int[10]=%d\n",
        (int)sizeof(af), (int)sizeof(ad), (int)sizeof(as),
        (int)sizeof(ac), (int)sizeof(aa));
}

int array[10];

main()
{
    test(0., 0., 0, 0, array);
    return 0;
}

%lint sizeof.c               (not BSD lint)
sizeof.c:
sizeof.c(4): warning: float type changed to double
sizeof.c(8): warning: array[10] type changed to pointer

% cc sizeof.c && ./a.out
sizeof args:   float=8  double=8  short=2  char=1  int[10]=4
sizeof autos:  float=4  double=8  short=2  char=1  int[10]=40


The "40" vs. the "4" shows the array parameter "adjustment".

And (float) is similarly adjusted form 4 to 8, since it is
actually passed as (double).

What I don't understand is why the (short) and (char) parameters
weren't adjusted to (int) for the same reason.
Either they should all have been left alone (desirable) or they
should all have been adjusted (correct).
The current BSD 4.3 behaviour isn't even consistent with itself.

Do other compilers get this adjustment right?

chris@trantor.umd.edu (Chris Torek) (02/16/88)

In article <16940@watmath.waterloo.edu> rbutterworth@watmath.waterloo.edu
(Ray Butterworth) writes:
>C performs this "helpful" adjustment of the programmer's
>misdeclaration with other parameters too. ...
>sizeof args:   float=8  double=8  short=2  char=1  int[10]=4
>sizeof autos:  float=4  double=8  short=2  char=1  int[10]=40
>
>The "40" vs. the "4" shows the array parameter "adjustment".

This one is `correct' according to the language definition;

>And (float) is similarly adjusted form 4 to 8, since it is
>actually passed as (double).

but this one is just a bug.

Vax PCC could get away without the internal adjustment, since the Vax's
float and double (F and D floating, not G and H) types are `compatible'.
That is, double just adds more mantissa bits.  Other compilers might
have to generate a `virtual local float', something like:

	f(x) float x; { ... }
becomes: f(_arg_x) double _arg_x; { float x = _arg_x; ... }
-- 
In-Real-Life: Chris Torek, Univ of MD Computer Science, +1 301 454 7163
(hiding out on trantor.umd.edu until mimsy is reassembled in its new home)
Domain: chris@mimsy.umd.edu		Path: not easily reachable

mouse@mcgill-vision.UUCP (der Mouse) (03/09/88)

In article <2305@umd5.umd.edu>, chris@trantor.umd.edu (Chris Torek) writes:
> In article <16940@watmath.waterloo.edu> rbutterworth@watmath.waterloo.edu (Ray Butterworth) writes:
>> C performs this "helpful" adjustment of the programmer's
>> misdeclaration with other parameters too. ...
>> sizeof args:   float=8  double=8  short=2  char=1  int[10]=4
>> sizeof autos:  float=4  double=8  short=2  char=1  int[10]=40
>> The "40" vs. the "4" shows the array parameter "adjustment".
> This one is `correct' according to the language definition;
>> And (float) is similarly adjusted form 4 to 8, since it is actually
>> passed as (double).
> but this one is just a bug.

I don't believe I just saw Chris say that.

K&R, page 186, fifth paragraph on that page.

					der Mouse

			uucp: mouse@mcgill-vision.uucp
			arpa: mouse@larry.mcrcim.mcgill.edu