Category talk:C: Difference between revisions

 
(One intermediate revision by one other user not shown)
Line 15:
: The only way to do a reference to a "thing" is a pointer indeed. In C this is explicit. In other languages it is not... But pointers are the way C calls "by reference", so function(&c) is a passing of c by reference... then the reference can be dereferenced... not simulation! --[[User:ShinTakezou|ShinTakezou]] 13:46, 31 May 2009 (UTC)
:: That's not call by reference. That's passing the address of the thing by value... ;-) —[[User:Dkf|Dkf]] 13:55, 31 May 2009 (UTC)
::: And what do you think that it works in other languages the "pass by reference"? In C it is explicit the fact that there are pointers at work. In other languages it is not explicit, it's hidden in layers of abstraction... but it is what it happens at the end of the games. No way to get out.
:::<lang c>int x;
int *rx;
x = 5;
rx = &x;</lang>
::: Now, rx can be used as a reference to x. Similarly, a lang like Perl has the "\" "operator", and the "semantic" (maybe the wrong word, I don't know) is the same of the pointers...
:::<lang perl>my @arr = ( 1, 2, 3 );
my $ref = \@arr;
a_function($ref); # the same as C, $ref is a reference, ... but it is passed "by value", i.e.
# there's no difference with the following
my $scalar = 12;
a_function($scalar);</lang>
:::: Of course they're all done as about the same thing when the bytes hit the processor. That's not the point. The point is that it is about the model(s) provided by the language itself; C (unlike C++) is strictly value-passing. (BTW, the Perl example is invalid here, since the parameter-passing model is more about how formal parameters are declared rather than how you write the calls; Perl supports multiple parameter modes.)
:::: More to the point is that you can't tell in C whether a formal argument that is of type <code>char *</code> is taking a string or a simulated "reference" to a char variable. This has actually been the source of bugs I've come across in peoples' code when doing maintenance coding, e.g., where someone had misused <code>scanf("%1s", &charVar)</code> and gotten away with it on big-endian platforms… —[[User:Dkf|Dkf]] 22:11, 31 May 2009 (UTC)
Anonymous user