typestar

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

  1. struct Point bundles two doubles.
  2. Fields are read and written with the . operator.
  3. A struct can be passed and returned by value.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Structs, step 19 of 25 in Pointers & memory.

← Previous Next →