Re: why cannot assign to function call



Mark Wooding wrote:
rurpy@xxxxxxxxx <rurpy@xxxxxxxxx> 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
call-by-reference?

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)
end

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

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)
end

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

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
by-value.
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;
.



Relevant Pages

  • Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!
    ... Even if it were, ->TOUCHES is an int, not a pointer. ... Passing a variable by value: ... Am I not passing a pointer by reference using multi indirection? ... config(obj_PASSCODE); ...
    (microsoft.public.vc.language)
  • Re: K&R2 exercise 1.19 "reversing a character array"
    ... So your string reversing function actually gets ... two pointer values to the start of the 'from' and 'to' arrays. ... unsigned long int ctr, end = length; ...
    (comp.lang.c)
  • Re: OK folks, corrected
    ... int function; ... since this tells the compiler that this pointer can be 1 or more ... since you can't have arrays of incomplete type. ...
    (comp.lang.c)
  • Re: function arguments and pointers revisited
    ... I have a structure that contains 2D arrays: ... and a function: int Trans{ ... This way gives me a pointer type error and I don't want to send the ... If the reason you don't want to pass the entire structure to the ...
    (comp.lang.c)
  • Re: pointers and array of pointers
    ... int main{ ... Pointer variables, like ordinary "int" ... function doesn't work as it's supposed to do, whereas myswap2 is doing ... is part of The Rule about arrays and pointers in C -- for more on ...
    (comp.lang.c)