why cannot assign to function call

rurpy at yahoo.com rurpy at yahoo.com
Sun Jan 11 04:59:13 CET 2009

Mark Wooding wrote:
> rurpy at yahoo.com <rurpy at yahoo.com> wrote:
>> What is the observable difference between converting an
>> array to a reference (pointer) to that array and passing
>> the reference by value, and passing the array by reference?
> For one:
> #include <stdio.h>
> static size_t foo(char v[]) { return sizeof v; }
> int main(void) {
>   char v[sizeof(char *) * 10];
>   puts(foo(v) == sizeof(char *) ? "pointer" : "reference");
>   return (0);
> }

You are saying that because the size of the argument
(10) is not available in the function, it cannot be

I think fortran is accepted as the archetypal call-by-
reference language and it does not automatically
supply argument size information to functions.  In
fortran, if the size of the argument is known at compile
time, the programmer explicitly declares the parameter
with the same size in the function.

      integer k(3)
      call mysub (k)
      write (unit=*, fmt=*) k(1),k(2),k(3)

      subroutine mysub (x)
      integer x(3)
      do 100, i=1,3
  100   x(i) = i

If not, he passes the size explicitly as an argument:

      integer k(3)
      call mysub (k, 3)
      write (unit=*, fmt=*) k(1),k(2),k(3)

      subroutine mysub(x, n)
      integer x(0)
      do 100, i=1,3
  100   x(i) = i

Obviously both these idioms translate directly into
C.  Here is the second:

     #include <stdio.h>
     void mysub (int k[], int n) {
        int i;
        for (i=0; i<3; i++) {
            k[i] = i; }
            return; }

      main() {
        int k[3];
        mysub (k, 3);
        printf ("%i,%i,%i\n", k[0],k[1],k[2]); }

So if fortran can be call-by-reference without the
compiler passing size information, I don't see why
the above C code can't be as well.

> For another:
> static void bar(char v[]) { char ch = 0; v = &ch; }
>   /* type error if arrays truly passed by reference */

v can be used as an array reference, e.g. "v[1] = 23"
exactly as in the pass-by-reference fortran example.  And
v can also be used as a local variable and reassigned.
If the first option was the only one, would you not
say C was definitely pass-by-reference (for arrays)?
If one could only use v as a pointer (i.e. access
the argument array as "*(v+1) = 23", then I would
say that arrays are not passed at all, only pointers
That both these options exist causes me to conclude
that, for arrays, parameter passing can be viewed
as either arrays by-reference or pointers by-value.

I don't understand what relevance type checking
has.  Since you are choosing to use v as a pointer,
one would not expect a type error, yes?

>> I guess the case for pass-by-value would be a little stronger because
>> one has to have "passing a pointer by value" anyway (since pointers
>> are first-class datatypes) and that can be used to describe passing
>> arrays (as you described).
> The difference is that the /callee/ function is different between the
> two cases.
> Also, notice that arrays in expressions turn into pointers in the same
> way, so function argument passing works the same way as assignment -- a
> hallmark of pass-by-value.

Not in all expressions as you yourself mentioned:
 int a[10], *ap;
 sizeof a;
 sizeof ap;

More information about the Python-list mailing list