codegurukool.com · Free coding tutorials & online compiler

Home › C Tutorial › Structs and pointers

Structs and pointers

Use -> on struct pointers.

Pass struct pointers to avoid copying. p->x equals (*p).x.

Example code

#include <stdio.h>
struct Pt { int x, y; };
void move(struct Pt *p) { p->x += 10; }
int main() {
  struct Pt a = {1, 2};
  move(&a);
  printf("%d %d\n", a.x, a.y);
  return 0;
}

Run this code in the online editor

← Nested structs and arrays of structsC unions →