Thanks Adak, I was just wondering how would the function react in the intern using 'copies'. Now, when you say "C always copies parameters..", I assume that other languages have other characteristics than C.
In PHP has to be something like this:
PHP Code:
<?php
$a = 2;
$b = 3;
//before swap
echo $a . ' ' . $b;
//after swap
foo($a,$b);
echo "\n" . $a . ' ' . $b;
function foo( &$p, &$q){
$tmp = $p;
$p = $q;
$q = $tmp;
}
?>
Let's do the same thing in C (I used Dev-C++ to write this code):
Code:
#include <stdio.h>
#include <stdlib.h>
void foo(int*, int*);
int main(int argc, char *argv[]){
int a = 2;
int b = 33;
int *p, *q;
p = &a;
q = &b;
printf("%d, %d\n",*p,*q);
foo(p, q);
printf("%d, %d\n",*p,*q);
system("PAUSE");
return 0;
}
void foo(int *a, int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}
Or
Code:
void foo(int*, int*);
int main(int argc, char *argv[]){
int a = 2;
int b = 33;
printf("%d, %d\n",a,b);
foo(&a, &b);
printf("%d, %d\n",a,b);
system("PAUSE");
return 0;
}
void foo(int *a, int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}
In C is more difficult to understand what's happening!!