Structs in C
Grouping related fields into one value type.
struct Point {
double x;
double y;
};
struct Point midpoint(struct Point a, struct Point b) {
struct Point mid;
mid.x = (a.x + b.x) / 2.0;
mid.y = (a.y + b.y) / 2.0;
return mid;
}
How it works
struct Pointbundles two doubles.- Fields are read and written with the
.operator. - A struct can be passed and returned by value.
Keywords and builtins used here
Pointdoublereturnstruct
The run, in numbers
- Lines
- 11
- Characters to type
- 180
- Tokens
- 64
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 40 seconds.
Step 1 of 3 in Structs, step 19 of 25 in Pointers & memory.