/* declare the Color type */ typedef struct { int red, green, blue; } Color; /* function to print a Color */ void printColor( Color *c ) { printf("%3d red, %3d grn, %3d blu\n", c->red, c->green, c->blue ); } /* function to construct a Color */ void setColor( Color *c, int r, int g, int b ) { if ( r<0 || r>255 ) r = 0; if ( g<0 || g>255 ) g = 0; if ( b<0 || b>255 ) b = 0; c->red = r; c->green = g; c->blue = b; } int main(int argc, char *argv[]) { /* declare and initialize two Colors */ Color grass, sky; setColor( &grass, 50, 200, 100 ); setColor( &sky, 100, 100, 250 ); /* print values */ printColor( &grass ); printColor( &sky ); system("pause"); return 0; }
Comments: The set function does not allow out of range values, but does no error reporting.