CS120 - Day 7 - Structs ================================== Update game.c example by - creating a Player struct - making an array of players - fix gameplay Structures - way of creating a data type with multiple data parts - two components: - define struct - declare variables struct sname { fields (variable declarations) } [var declarations] ; - referencing - dot operator for struct field vars - arrow operator for struct * vars - equivalence: sptr->part == (*sptr).part highest precedence: () [] . -> - initialize in declaration - use {} and list of values for each part - if fewer values than parts, remainder init to 0 or NULL - passed by VALUE to functions by default! - can pass whole structure, or just an element of structure - good reason to pass with pointer but const protect struct SSNum { int n1, n2, n3; } ssn; scanf("%d-%d-%d", &ssn.n1, &ssn.n2, &ssn.n3); printf("%03d-%02d-%04d\n", ssn.n1, ssn.n2, ssn.n3); - typedef: must put typename at end of declaration typedef struct { fields; } typename; typename mything; See code files: structsFail.c, structs.c, structs2.c TOOL: indent - use it to automatically format your code consistently > indent mycode.c reformat and overwrite your file, creates back-up? > indent mycode.c -o mycodePretty.c - there are lots of customization options, look in the man pages - personal favorites: -kr for kernigan & ritchie style -i4 for level indents of 4 spaces The Problem: Create a simplified database application for course listings. Each course in the database will have a unique two part number in accordance with the JHU system: ddd.nnn where ddd is the department number ranging from 001 to 700 and nnn is the course number ranging from 100 to 899, inclusive. Each course will also have a set of area designators (any combination of HSNEQW, including none at all), a number of credits which is a multiple of .5 between 0.5 and 4.5 inclusive, and a title which is at most 30 characters. Operations: - add a new course to the database such as: 600.107 [E] Intro to Programming in Java (3.0) - display an existing course based on course number (ddd.nnn format) - display all the courses - change a course name, based on course number (ddd.nnn) - delete a course, specified by course number (ddd.nnn) - display all courses with particular area designators (one or more)