Simvik

Move a point

Change the caller's struct through a pointer. An easy embedded C problem (Structs, enums and typedef), run and graded in the browser.

easy · Structs, enums and typedef · structs · pointers · arrow

Move the point that p points to by dx across and dy up. The caller's point must change.

Starter code

typedef struct {
  int x;
  int y;
} point_t;

void move_by(point_t *p, int dx, int dy) {
}

Example tests

Move right and up

point_t p = { 1, 1 };
move_by(&p, 2, 3);
printf("%d %d\n", p.x, p.y);

Expected output:

3 4

Move left and down

point_t p = { 0, 0 };
move_by(&p, -5, -1);
printf("%d %d\n", p.x, p.y);

Expected output:

-5 -1

Learn it first

  • Structs, enums and typedef · Grouping values into structs, the dot and the arrow, naming choices with enum, and shorter names with typedef.

More Structs, enums and typedef problems