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 4Move left and down
point_t p = { 0, 0 };
move_by(&p, -5, -1);
printf("%d %d\n", p.x, p.y);Expected output:
-5 -1Learn 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
- Build a struct · Fill in a struct's members and return it.
- Distance between two points · Read the members of two structs passed by value.
- Average the valid readings · Loop over an array of structs and use only some of them.
- The next traffic light colour · Step through the states of an enum.
- Count the LEDs that are on · Combine a struct, an enum member and a loop.