CS 214 / Project I, more types ============================== Project I has been posted you can work with anyone from any section, but only with one person Make sure you and your partner explicitly say who you are working with enum ---- a way to express a small set of values enum direction { north, south, east, west }; enum color { red, green, blue, yellow, white, black, secret_color }; these create a new type (enum direction) and several "enumerators" (named values such as north, south, etc.) these types are actually int the enumerators are just names for ints (by default, numbered starting from 0) enum direction heading = north; if (heading == south) { ... } switch (heading) { case north: ... case south: ... case west: ... default: ... } because enums are just ints, the compiler will "helpfully" insert casts as needed int x = south; enum color background = east; if (red == 0) { ... } background = 25; if you really want, you can specify the values enum foo { bar, baz = 20, quux }; the declaration above creates: bar == 0 baz == 20 quux == 21 enum { open, closed } door = open; door = closed; for (enum direction d = north; d <= east; d++) { // loop through all directions } struct ------ bundles multiple data items into a single object struct point { double x; double y; }; // declares a type called "struct point" // two accessors: x and y (of type double) struct point p; p.x = 1; p.y = -0.5; struct point *q; q = &p; q = (struct point *) malloc(sizeof(struct point)); to access a field of the object q points to: (*q).x q->x q = (struct point *) malloc(100 * sizeof(struct point)); q[0] is a struct point q[0].x struct rectangle { struct point topleft; struct point bottomright; enum color fill; char *name; }; struct rectangle r; r.topleft.x = 1; r.name = "mister rectangle"; r.color = blue; initializing struct variables struct point p = { 1, 2.5 }; struct rectangle r = { p, { 0, 0 }, blue, "example" }; struct polygon { int num_vertices; struct point *vertices; }; void draw_point(struct point *p); void foo (struct polygon *p) { for (int i = 0; i < p->num_vertices; i++) { draw_point(&(p->vertices[i])); } } unlike array variables, struct variables do not decay to pointers structs are passed by value struct point p = { 1, 2 }; struct point q; q = p; // same as q.x = p.x; q.y = p.y; void set_origin (struct point p) { p.x = 0; p.y = 0; } // does not actually do anything // because p is a local variable (it gets a copy of its argument) struct point q = { 10, 20 }; set_origin(q); q.x is still 10 void actually_set_origin (struct point *p) { p->x = 0; p->y = 0; } struct point q = { 10, 20 }; actually_set_origin(&q); // now q.x == 0 struct vector { double e[3]; }; // a value of type struct vector is indistinguishable from an array of 3 // doubles (at run-time) struct vector v = { { 1, 2, 3 } }; struct vector w; w = v; // this actually works! typedef ------- allows us to make a new name for an existing type typedef int age; // creates a new name, age, which is a synonym for int typedef struct point point_t; typedef double vector[3]; // creates a name vector that is a synonym for double [3] vector v; // same as double v[3]; typedef int (*comparison_fun)(void *, void *); void bubble_sort(void *, size_t, size_t, comparison_fun);