Ausgabe eines c-Codes wie bei call by reference [closed]
Mit diesem C-Code:
int x=12,y=10;
void tswap(int pa, int pb) {
int tmp;
tmp=pa;
pa=pb;
pb=tmp;
x=x+pa;
x=x-pb;
y++;
printf("%d %d %d %d\n",pa,pb,x,y);
}
int main() {
int a=4;
tswap(x,a);
printf("%d %d %d\n",x,y,a);
return 0;
}
Ich muss herausfinden, was es drucken würde, wenn C Call-by-Reference verwendet.
Hier sind meine Erwartungen.
Zuerst
x = 12 y = 10 und a = 4
after tswap(12,4):
pa=x=12 pb=a=4
after tmp=pa:
tmp=pa=x=12
-----------
after pa=pb:
tmp=x=12
pa=pb=a=4
-------------
after pb=tmp:
pb=tmp=x=12
pa=a=4
after x=x+pa
x=16
------------
and after x=x-pb and y++
x=4,y=11
Also denke ich, x = 4, pb = 12, pa = 4, y = 11; aber die antwort sagt -4 12 -4 11. Wo irre ich mich?