Example 1 — Move a point through a pointer
The function edits the caller's struct.
#include <stdio.h>
struct Point { int x, y; };
static void move(struct Point *p, int dx, int dy) {
p->x += dx;
p->y += dy;
}
int main(void) {
struct Point pt = {1, 2};
struct Point *p = &pt;
p->x = 5;
printf("(%d, %d)\n", pt.x, (*p).y);
move(&pt, 2, 3);
printf("(%d, %d)\n", pt.x, pt.y);
return 0;
} How It Works
p->x = 5changespt.x.(*p).yis the long form ofp->y.movereceives the address, so changes persist inmain.
