CSC161 2010F, Class 43: Structures Overview: * Questions on Assignment 8. * Structured types in C. * Lab. Admin: * Reading for Wednesday: K&R 6.5 (redo) and 6.6. * Soda question. Dr. Pepper 3 Diet Dr. Pepper 0 Seven Up 6 Diet Seven Up 1 A&W Rootbeer 6 Diet A&W Rootbeer 0 Sunkist Orange 6 Diet Sunkist Orange 1 Diet Rite 0 * EC for Thursday extra (some grad student from somewhere). Science 3821 at 4:30, food at 4:15 * EC for Rosenfield Corn Symposium activites. Questions on Assignment 8 * Can we use -w ### instead of -w###? * Yes. * Do we need to support both? * No. * How do I ask whether the first two characters of a string are "-w". if (strncmp (str, "-w", 2) == 0) ... * How do I say "skip the first two letters of str and turn the rest into an integer"? atoi (str+2) * Can you grade HW7 first, or at least give a sample solution? I'll try * Does "continue" in a for loop still do the increment? Yes, I believe so. * How can we compare alphabetically? strcmp * How might I do X? See K&R * How can I deal with the issue that I might have user-specified string sizes (-w) and numbers of lines (-l)? Write a helper function of the form void sort_helper (int stringsize, int lines, ...) { char strings[lines][stringsize]; ... } * Boy, that wastes a lot of space. Is there anything better? void sort_helper (int stringsize, int lines, ...) { char *strings[lines]; ... strings[i] = alloc(size); ... } * Where do I get the alloc function? K&R! Structs (Structured Types) in C * Design problem: We have values that naturally "go together" When we're talking about a point, the x and y coordinates form a natural pair. When we're talking about a student, the first name, last name, userid, studentid, and GPA form a natural grouping * Not all groupings are homogeneous * We could treat a point as a two-element array of ints (or doubles) * But students are heterogeneous: Strings plus numbers * And named points are also heterogeneous * Even when groupings are homogeneous, arrays can be confusing In C, and in many other languages, we have "structured types" that address these issues * User gets to define them * Group together data * Name the parts (fields) struct NAME { TYPE FIELDNAME; TYPE FIELDNAME; TYPE FIELDNAME; }; E.g. struct point { int x; int y; }; * The thing above defines the type "struct point" How do we declare variables of this type? struct point start; * start is a variable of type "struct point" or struct point { int x; int y; } thesis; * struct point is a type with two integer fields, x, and y * thesis is a variable of type "struct point" Once we've declared a variable of that type, we can access the component fields. thesis.x = 5; thesis.y = 23; distance = sqrt (square(start.x - thesis.x) + square(start.y - thesis.y)); Issue: Some people find "struct foo" to be too much to write (and too hard to modify). typedef struct point { int x; int y; } pt; * "pt" is another name for the type "struct point" typedef TYPE NAME typedef int boolean; Questions one might ask about structured types: * Can I assign them? * Can I pass them as parameters? * Can I coerce them into each other? * And what happens if I can? * How much space do they require? * How are they laid out in memory?