#include void bad_swap (int x, int y); void swap (int *x, int *y); int main () { int a = 1, b = 2; int *p, *q, *r; p = &a; q = &b; r = &a; printf("The following commands were given:\n"); printf("1. int a = 1, b = 2\n"); printf("2. int *p, *q, *r\n"); printf("3. int p = &a\n"); printf("4. int q = &b\n"); printf("5. int r = &a\n"); printf("\nHere are the values prior to the first operation:\n"); printf(" a = %10d b = %10d\n", a, b); printf(" p = %10d q = %10d r = %10d\n", p, q, r); printf(" *p = %10d *q = %10d *r = %10d\n", *p, *q, *r); printf("\nThe following is an attempt to assign the value of 'b' to 'a' using pointers:\n"); p = q; printf("The following command was given:\n"); printf("1. p = q\n"); printf("\nHere are the values after the first operation:\n"); printf(" a = %10d b = %10d\n", a, b); printf(" p = %10d q = %10d r = %10d\n", p, q, r); printf(" *p = %10d *q = %10d *r = %10d\n", *p, *q, *r); printf("\nAs can be seen from the numbers above, all that happened was\n"); printf("that 'p' became a pointer to 'b.' The value of 'a' remained unchanged.\n"); printf("Press RETURN to continue..."); getchar(); printf("\nAll values have been returned to normal.\n"); p = r; printf("\nHere are the values prior to the second operation:\n"); printf(" a = %10d b = %10d\n", a, b); printf(" p = %10d q = %10d r = %10d\n", p, q, r); printf(" *p = %10d *q = %10d *r = %10d\n", *p, *q, *r); printf("\nThe following is a demonstration that variables are passed by\n"); printf("value to functions; thus, preventing the function from making\n"); printf("changes to the ACTUAL variable.\n"); printf("\nThe following function call was issued:\n"); printf("1. bad_swap (a, b)\n"); bad_swap (a, b); printf("\nHere are the values after the second operation:\n"); printf(" a = %10d b = %10d\n", a, b); printf(" p = %10d q = %10d r = %10d\n", p, q, r); printf(" *p = %10d *q = %10d *r = %10d\n", *p, *q, *r); printf("\nAs can be seen from the numbers above, nothing happened to the\n"); printf("ACTUAL variables within the main program.\n"); printf("Press RETURN to continue..."); getchar(); printf("\nAll values have been returned to normal.\n"); printf("\nHere are the values prior to the third operation:\n"); printf(" a = %10d b = %10d\n", a, b); printf(" p = %10d q = %10d r = %10d\n", p, q, r); printf(" *p = %10d *q = %10d *r = %10d\n", *p, *q, *r); printf("\nThe following is a demonstration of a properly executed\n"); printf("swap function. This implementation employs the use of pointers\n"); printf("passed to the function, so that the addresses can be changed.\n"); printf("\nThe following function call was issued:\n"); printf("1. swap (p, q);\n"); swap (p, q); printf("\nHere are the values after the third operation:\n"); printf(" a = %10d b = %10d\n", a, b); printf(" p = %10d q = %10d r = %10d\n", p, q, r); printf(" *p = %10d *q = %10d *r = %10d\n", *p, *q, *r); printf("\n\n\n\n\n"); return (0); } void bad_swap (int x, int y) { int temp; printf("This line occurs inside the function before swap: x(a) = %d y(b) = %d\n", x, y); temp = x; x = y; y = temp; printf("This line occurs inside the function after swap : x(a) = %d y(b) = %d\n", x, y); return; } void swap (int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; return; }