[net.lang.c] passing arrays by value

rjv@ihdev.UUCP (ron vaughn) (05/16/85)

*** EGASSEM RUOY HTIW ENIL SIHT ECALPER ***

quick and simple:  in c i wanted to pass an array by value.  oops.  hmmmm.
ponder ponder.  compiler says: an array IS a pointer.  quick
and dirty workaround -- shove the array in a struct (that does nothing
but contain the array) and pass the struct around.  worked fine.  looked
ugly.  anyone have any better ideas??  am i missing something obvious here??


	take my code, please.

	ron vaughn	...!ihnp4!ihdev!rjv

rjv@ihdev.UUCP (ron vaughn) (05/16/85)

*** EGASSEM RUOY HTIW ENIL SIHT ECALPER ***

i said earlier:
>quick and simple:  in c i wanted to pass an array by value.  oops.  hmmmm.

i've already received a few letters saying "are you sure you mean by value??"
i should have explained, i was doing the classic "knights tour" problem,
using branch and bound techniques.  classic b&b differs from regular
ol' backtracking, it builds a big tree, and keeps a local version
of the board at EACH node in the tree.  hence each recursive call to
place a knight on the board must pass the board by value.  
on the other hand, with most backtracking you can keep one version of 
the board and work with it only.
these comments on b&b and backtracking algorithms are generalizations.
there are exceptions, b&b can be forced to use pass by reference, but
pass by value is much cleaner. (who cares about memory...)

it is kind of interesting to compare C to, say, pascal, where
you use "var" (or whatever, i hate pascal) to explicitly state you
want pass by reference.  from a user viewpoint, pascal makes sense,
you tell it how you are passing.  in C it looks like everything
is passed by value (so you wind up passing &thing to get pass by reference)
except for arrays, which apparently can't be passed by value.
this *almost* makes sense...

does this really matter?  am i still missing something??  is there
a reason arrays are special -- the "array IS a pointer" argument, perhaps??
is the array==pointer at the heart of C and thinking anything else
a valid reason for public flogging???

	keep those cards and letters coming,

	ron vaughn	...!ihnp4!ihdev!rjv

gwyn@Brl.ARPA (VLD/VMB) (05/17/85)

An array is not a pointer, but your approach to passing an array by
value (embedding it in a struct) is undoubtedly the simplest way to
do that.