Pointers
Addresses, dereferencing, and changing a value from a function.
A pointer stores a memory address. & gives the address of a variable, and * reads or writes the value at an address.
C
#include <stdio.h>
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int main(void) {
int x = 1, y = 2;
swap(&x, &y);
printf("%d %d\n", x, y);
int arr[] = {10, 20, 30};
int *p = arr;
printf("%d %d\n", *p, *(p + 2));
return 0;
}Output
2 1 10 30
C passes arguments by value, so swap needs pointers to change the caller's variables. Adding 2 to a pointer moves it forward by two elements, not two bytes.