You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.3 KiB
C
61 lines
1.3 KiB
C
/*
|
|
Structures allow us to construct a collection or sequence of variables, which can be of any type
|
|
*/
|
|
#include <stdio.h>
|
|
|
|
//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
|
|
|
|
//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;
|
|
}
|