/* Structures allow us to construct a collection or sequence of variables, which can be of any type */ #include //ask dan typedef unsigned char byte; //alternative method is wrapping struct in a typedef //The compiler doesn't care typedef struct Color { /* data */ byte Red; byte Green; byte Blue; } Color; struct Pixel { /* data */ float X; float Y; Color color; }; typedef struct Pixel Pixel; void print_pixel(Pixel p) { puts("Pixel Location"); printf("X:%2f, Y:%2f\n", p.X, p.Y); puts("Pixel color"); printf("rgb(%d, %d, %d)\n", p.color.Red, p.color.Green, p.color.Blue); } int main(int argc, char const *argv[]) { //In C the compiler places structs in a different namespace //This requires us to use the struct keyword to have the compiler look up struct definitions //like so //struct Pixel p; //struct Color c; // but we can instead introduce the structs via typedefs and leave off the struct keyword //Also we can initialize structs with the following Color c = {255, 128, 0}; Pixel p = {10.0f, 20.0f, c}; // Color c = { 255, 128 }; //Trailing members are zero initialized printf("Just so you know you're 3 byte struct for Color will be padded. Padded size in bytes: %d\n", (int)sizeof(Color)); printf("Size of Pixel: %d\n", (int)sizeof(Pixel)); //We can access members of structs via dot notation like so float x = p.X; //We can use assignment to update a member directly c.Blue = 255; Pixel o = {12.0f, 22.0f, c}; print_pixel(p); print_pixel(p); return 0; }