61 lines
951 B
C
61 lines
951 B
C
/*
|
|
tp-swap.c: TecPro Beispielprogramm
|
|
Kurzbeschreibung: "call by reference / call by value"
|
|
C.Koch | HS-Emden-Leer | 19.12.2010
|
|
*/
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
int swapRef(int* a, int* b);
|
|
int swapVal(int a, int b);
|
|
int swapMem(int* a, int* b);
|
|
|
|
void main(void)
|
|
{
|
|
/* auto -> Stack | static -> Datensegment*/
|
|
static int a,b;
|
|
|
|
a = 10; b = 20;
|
|
|
|
swapVal(a,b);
|
|
printf("Var \t a =%i \t b= %i \n",a, b);
|
|
swapRef(&a, &b);
|
|
printf("Var \t a =%i \t b= %i \n",a, b);
|
|
swapMem(&a, &b);
|
|
printf("Var \t a =%i \t b= %i \n",a, b);
|
|
}
|
|
|
|
int swapRef(int* a, int* b)
|
|
{
|
|
int temp;
|
|
|
|
temp = *a;
|
|
*a = *b; *b = temp;
|
|
|
|
return temp;
|
|
}
|
|
|
|
int swapVal(int a, int b)
|
|
{
|
|
int temp;
|
|
|
|
temp = a;
|
|
a = b; b = temp;
|
|
|
|
return temp;
|
|
}
|
|
|
|
/* Aufgabe: Wie muss die folgende Funktion geändert werden,
|
|
falls die Variablen a und b auf dem Stack liegen und nicht im Datensegment? */
|
|
int swapMem(int* a, int* b)
|
|
{
|
|
int temp;
|
|
|
|
temp = *a;
|
|
*a = *(a+1); *(a+1) = temp;
|
|
|
|
return temp;
|
|
}
|
|
|