We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Structs

Click to play video

So far all we've seen are the simple (non-collection) types in C. However, stuff like this can get really annoying:

int main() {
    int x_1 = 1;
    int y_1 = 2;
    int z_1 = 3;
    int x_2 = 4;
    int y_2 = 5;
    int z_2 = 6;

    int dist = distance(x_1, y_1, z_1, x_2, y_2, z_2);
    printf("Distance: %d", dist);
}

Because our distance function starts to look... ridiculous.

int distance(int x_1, int y_1, int z_1,
             int x_2, int y_2, int z_2)
{
    // a lot of numbers
}

We also run into a new problem: In C, we're only allowed to return a single value from a function. This doesn't work:

int int int scale_coordinate(int x, int y, int z, int scale) {
    return x * scale, y * scale, z * scale;
    // Error! Too many values to return
}

Structs solve this. Here's an example of the syntax:

struct Human {
    int age;
    char *name;
    int is_alive;
};

Assignment

Sneklang has built-in support for graphics programming (and vows to never gouge game devs...).

Define a new struct called Coordinate in coord.h. Remember, .h files are for declarations of types and function prototypes. The Coordinate struct should have three fields: