Build a struct
Fill in a struct's members and return it. An easy embedded C problem (Structs, enums and typedef), run and graded in the browser.
easy · Structs, enums and typedef · structs · typedef
point_t groups an x and a y coordinate into one type.
Return a point_t whose x is x and whose y is y.
Starter code
typedef struct {
int x;
int y;
} point_t;
point_t make_point(int x, int y) {
point_t p = { 0, 0 };
return p;
}Example tests
Positive coordinates
point_t p = make_point(3, 4);
printf("%d %d\n", p.x, p.y);Expected output:
3 4Negative coordinates
point_t p = make_point(-2, -9);
printf("%d %d\n", p.x, p.y);Expected output:
-2 -9Learn 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
- Distance between two points · Read the members of two structs passed by value.
- Move a point · Change the caller's struct through a pointer.
- 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.