A pointer is a variable that holds an address. *a means: the thing at that address.
A pointer holds the address of another object
Level 4 of 13 · interactive GDB exercise · runs in your browser
Connect a pointer's value to the address of the object it refers to, and watch a write through a pointer change a variable in another frame.
Here a and b point at x and y over in main. Follow the arrows on the right.
Step through swap and watch x and y in main change.
You work in a real GDB session on the program below. A breakpoint on swap was set and the program was started; it is paused at the first line of swap.
#include <stdio.h>
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
int main(void) {
int x = 10;
int y = 20;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
main.c, compiled with gcc -g -O0 -std=c11 -Wall -fno-omit-frame-pointer -fno-stack-protector -no-pie -fno-pie for 32-bit x86 Linux.
print <expr>nextframe <n>, up, downx/<n><fmt><size> <addr>info argsptype <expr>print x inside swap fails with No symbol "x" in current context. x belongs to main. Select that frame with up, or use print *a.a (an address) with *a (the int stored there). Print both, and compare with the conceptual view: the box holds the address, the arrow leads to the int.