I am trying to compare how different functions work and I do not see whats wrong here. I expect to see 100 and 200 on the printf after the second function call and I get 2...2 no matter what I do (changed it from + to *). Can someone look and let me know the problem? Thanks.
Code:
void swap(int *swapa, int *swapb);
int swap2(int swapa);
main here....
int aa = 1;
int bb = 2;
/*This works fine*/
printf("%d is aa before function and %d is bb before function\n",aa, bb);
swap(&aa, &bb);
printf("%d is aa after function and %d is bb after function\n",aa, bb);
/*Here is where problem is*/
swap2(aa);
printf("%d is aa now\n", aa); //
swap2(bb);
printf("%d is bb now\n", aa);
functions here...
void swap(int *swapa, int *swapb)
{ /*params are int* instead of int*/
int temp;
temp = *swapa; /* use * to follow the pointer back to the caller's memory*/
*swapa = *swapb;
*swapb = temp;
}
int swap2(int swapa)
{
int temp =100;
int result = swapa + temp;
return (result);
}