Hello:
I understand C may only change an address via function. However I cannot seem to make that happen. Specific to the code below..."Why doesn't the function SwapAdd modify the addresses in function main?" "What needs to change in order to do so?"
Thanks!
Code:
#include <stdio.h>
#include <stdlib.h>
int SwapAdd (int* ptr1, int* ptr2);
int main (void) /* begin function main */
{
/* Variable Declarations */
/*-----------------------*/
int num1 = 5, num2 = 7;
int *num1Ptr = &num1, *num2Ptr = &num2;
printf ("\n This program swaps pointers to swap addresses \n\n");
printf ("\n &num1 address is %x\n", &num1);
printf ("\n num1Ptr address is %x\n", num1Ptr);
printf ("\n &num2 address is %x\n", &num2);
printf ("\n num2Ptr address is %x\n", num2Ptr);
printf ("\n &num1Ptr address is %x\n", &num1Ptr);
printf ("\n num1 is %d and num2 is %d\n", num1, num2);
SwapAdd (num1Ptr, num2Ptr); /* need to pass pointers */
printf ("\n\n After SwapAdd...\n\n");
printf ("\n &num1 address is %x\n", &num1);
printf ("\n num1Ptr address is %x\n", num1Ptr);
printf ("\n &num2 address is %x\n", &num2);
printf ("\n num2Ptr address is %x\n", num2Ptr);
printf ("\n &num1Ptr address is %x\n", &num1Ptr);
printf ("\n num1 is %d and num2 is %d\n", num1, num2);
getchar(); /* Wait for character. */
return 0;
}/* end function main */
/* Function(s) */
/*--------------*/
int SwapAdd (int* ptr1, int* ptr2)
{
int temp;
temp = ptr1;
ptr1 = ptr2;
ptr2 = temp;
return 0;
}/*end SwapAdd */