Example: Function Variable Scope

Broken code:

#include <stdio.h>
void swap(int a, int b)
int main(void)
{
    int x = 1;
    int y = 2;
    printf("x is %i, y is %i\n", x, y);
    swap(x,y);
    printf("x is %i, y is %i\n", x, y);
}
void swap(int a, int b)
{
    int tmp = a;
    a = b;
    b = tmp;
}

A fixed swap() function needs to use pointers to modify the original x and y variables:

#include <stdio.h>
void swap(int *a, int *b)
int main(void)
{
    int x = 1;
    int y = 2;
    printf("x is %i, y is %i\n", x, y);
    swap(&x,&y);
    printf("x is %i, y is %i\n", x, y);
}
void swap(int *a, int *b) // Address of x and y
{
    int tmp = *a; // Store the value at the address in a in tmp
    *a = *b; // Store the value at the address in location b in the value at location a
    *b = tmp; // Store the value in tmp in the value at location b
}