C functions and pointer parameters

C passes arguments by value, including pointer arguments. Copying an address into a parameter still lets the function access the original object at that address.

C
#include <stdio.h>
#include <assert.h>
void swap(int *a, int *b) {
    int temporary = *a;
    *a = *b;
    *b = temporary;
}
int main(void) {
    int x = 3, y = 8;
    swap(&x, &y);
    printf("%d %d\n", x, y);
    swap(&x, &x);
    assert(x == 8 && y == 3);
    return 0;
}
Read the full lesson