CS110_07c_enum+struct - Enum & Structures Jack...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Enum & Structures Jack Tumblin jet@cs.northwestern.edu EECS110: 7c . (Recall) Dynamic Allocation main() { int k,kmax,*pKey,*pDone; ... pKey = (int *)malloc(kmax*sizeof(int)); if(NULL==pKey) return(-1); // error?! Bye! for(k=0; k<kmax; k++) { Use sizeof() to find pKey[k] = rand(); } how many bytes. pDone = pKey; --Variables are OK here! ... (more work with pKey) ... free(pDone); } malloc(n) function: reserve a block of n bytes (Block has no name) ERRORS? malloc() returns NULL pointer (0) (Recall) Dynamic Allocation main() { int k,kmax,*pKey,*pDone; ... pKey = (int *)malloc(kmax*sizeof(int)); if(NULL==pKey) return(-1); // error?! Bye! for(k=0; k<kmax; k++) { pKey[k] = rand(); } malloc function reserves pDone = pKey; a block of n bytes ... (more work with pKey) ... free(pDone); } malloc returns pointer; ALWAYS typecast to the proper type. (Block has no name) Only pKey knows where to find the memory block. Change pKey? Lost! pKey If malloc() succeeds, then allocated memory is static; static always reserved for you, always valid, never forgets, until you free() it. main() (Recall) Dynamic Allocation { int k,kmax,*pKey,*pDone; ... pKey = (int *)malloc(kmax*sizeof(int)); if(NULL==pKey) return(-1); // error?! Bye! for(k=0; k<kmax; k++) free()needs only the free() { pKey[k] = rand(); address of the memory } block; it doesn't care pDone = pKey; ... (more work with pKey) ... free(pDone); } about variable names free(ptr) function: release a block of bytes at address ptr. ptr Chapter 12 should be named `Structures' C has just four basic (`intrinsic') types: char int float double We learned 2 more (`derived') types: arrays , pointers C has just three more derived types, but only one (structures) is really important: struct (structures--roots of C++ & more...) enum (enumerated types; mildly useful) union (archaic; can be dangerous) Enumerated Types A Variable holds a value, one from a set: set An int value? choose a set of 2+32 integers, An float value? choose from a set of 2+32 floats, A char value? from set of 2+8==256 characters... (The set for a 'string variable'? the set of all strings...) A Definition: the `data type' selects: Which set? But what if you want a non-numerical set, such as {Mon, Tues, Wed, Thu, Fri, Sat, Sun} ? (HINT: we just `enumerated' all the allowable values) Enumerated Types First Method: Use preprocessor directive #define and an int variable: #define #define #define #define #define #define #define SUN MON TUE WED THU FRI SAT 0 1 2 3 4 5 6 #define maps our desired set to int values. (Programmer must keep `today' and `tomorrow' integers within the meaningful 0-6 range). int today, tomorrow; today = SAT; tomorrow = (today+1)%7; Enumerated Types Second Method: Define a new `enum' data type. enum typedef enum weekdayT // define a new data type { Sun, Mon, Tues, Wed, Thu, Fri, Sat }weekdayT; // named `weekdayT' Name of our new data type weekdayT today,tomorrow; // declare vars today = Sat; tomorrow = (today+1)%7; // init the vars: // computed as int Enumerated Types Second Method: Define a new `enum' data type. enum typedef enum weekdayT // define a new data type { Sun, Mon, Tues, Wed, Thu, Fri, Sat }weekdayT; // named `weekdayT' weekdayT today,tomorrow; // declare vars Declare some variables of type weekdayT today = Sat; // init the vars: tomorrow = (today+1)%7; // computed as int Enumerated Types Second Method: Define a new `enum' data type. enum typedef enum weekdayT // define a new data type { Sun, Mon, Tues, Wed, Thu, Fri, Sat }weekdayT; // named `weekdayT' weekdayT today,tomorrow; // declare vars Initialize those variables of type weekdayT today = Sat; // init the vars: tomorrow = (today+1)%7; // computed as int Enumerated Types Mostly a notational convenience: it doesn't DO much! Works just like #define statements: 0 1 2 3 4 5 6 typedef enum weekdayT { Sun, Mon, Tues, Wed, Thu, Fri, Sat }weekdayT; enum variables given int values internally No restrictions on value: today = 578 is allowed (but meaningless). Modulus (%) can help... Chapter 12: Structures Arrays let us group together variables, BUT all array elements have the same data type. How can we make a group of variables with DIFFERENT types? char, int, char, float, char*,... In C, it's called a `structure' ... Data Structures Data Structure: `a named group of variables' Lets you tie together related data of unrelated types: Example: we want a (worker) data structure to tie together a string for `NAME', a float for `Salary', a char array for `Social Sec#', and an int for deductions. NAME Salary 250.00 10.00 20.00 Social Sec# 001-34-8902 022-85-7741 001-02-0837 Ded. 1 7 1 0 staff[0] staff[1] 1 staff[2] 2 staff[3] 3... ... Ebenezer Scrooge Bob Cratchit Phineas Fogg ... ... ... ... Data Structures A USER-DEFINED Data Type (!) It groups together related data: (e.g. worker's info) Assembled from existing data types (char,float,int...) YOU define this new data type to suit your program! NAME Salary 250.00 10.00 20.00 Social Sec# 001-34-8902 022-85-7741 001-02-0837 Ded. 1 7 1 0 staff[0] 1 staff[1] staff[2] 2 staff[3] 3... ... Ebenezer Scrooge Bob Cratchit Phineas Fogg ... ... ... ... (Recall) Data Types We already know these basic data types: types char, int, float, double, enum, pointer-to-..., array-of-... The data type defines the set of allowable values of a variable (an int variable can't have a value of 'H', etc.) A variable is a named place-holder in memory, and its value is restricted to just one type. (Recall) Data Types We already know these basic data types: types char, int, float, double, enum, pointer-to-..., array-of-... A data type defines the allowable values of a variable (an int variable can't have a value of 'H', etc.) A variable is a named place-holder in memory; its value is restricted to just one type We have defined our own data types before: typedef enum meetT { mon,tues,wed,fri } meetT; int main() { meetT today; (Recall) Data Types We already know these basic data types: types char, int, float, double, enum, pointer-to-..., array-of-... A data type defines the allowable values of a variable (an int variable can't have a value of 'H', etc.) A variable is a named place-holder in memory; its value is restricted to just one type We have defined our own data types before: typedef enum meetT { mon,tues,wed,fri } meetT; int main() { meetT today; CAREFUL! Don't confuse data type and variables ! Using Data Structures You create & use data structures in 3 crucial steps: New! 1) Define the data structure (the new data type): New! 2) Declare variables of the new data type, 3) Initialize & use variables of the new data type. NAME Salary 250.00 10.00 20.00 Social Sec# 001-34-8902 022-85-7741 001-02-0837 Ded. 1 7 1 0 staff[0] 1 staff[1] staff[2] 2 staff[3] 3... ... Ebenezer Scrooge Bob Cratchit Phineas Fogg ... ... ... ... New! 1) Define a Data Structure New! typedef struct workerT //*****employee record ***** { char *name; // string constant float salary; // in dollars char ssn[11]; // xxx-xx-xxxx int ded; // tax deductions } workerT; `define a new data type for me' int main() { `it is a data workerT emp1,staff[30]; structure, and...' } emp1.name = "Bob Cratchit"; emp1.salary = 10.00; strncpy(emp1.ssn,"022-85-7741",11); emp1.ded = 7; New! New! 1) Define a Data Structure New! typedef struct workerT //*****employee record ***** { char *name; // string constant float salary; // in dollars char ssn[11]; // xxx-xx-xxxx int ded; // tax deductions } workerT; int main() { `The name of the new data type is ...' workerT emp1,staff[30]; emp1.name help Bob Cratchit"; data T suffix to = " remind you that the emp1.salary a data type structure workerT=is10.00; type, not"a variable; variable strncpy(emp1.ssn,"022-85-7741 ,11); emp1.ded = 7; ?Named in TWO places?!? (see pg 805 in book) } YES: you'll need this later for structures that point to each other... New! 1) Define a Data Structure New! typedef struct workerT //*****employee record ***** { char *name; // string constant float salary; // in dollars char ssn[11]; // xxx-xx-xxxx int ded; // tax deductions } workerT; int main() The data structure is a collection of these `members' { workerT emp1,staff[30]; --all must have=a "Bob and a name, ; any other variables, type Cratchit" like emp1.name --(but these are `sub-variables' within the type `workerT') emp1.salary = 10.00; strncpy(emp1.ssn,"022-85-7741",11); emp1.ded = 7; } 2) Declare Variables of new Type typedef struct workerT //*****employee record ***** { char *name; // string constant float salary; // in dollars char ssn[11]; // xxx-xx-xxxx int ded; // tax deductions } workerT; `workerT' now acts as a data type, int main() just as char, int, float, etc. { (We declared a single var. `emp1' workerT emp1,staff[30]; and an array named `staff'.) emp1.name = "Bob Cratchit"; emp1.salary = 10.00; strncpy(emp1.ssn,"022-85-7741",11); emp1.ded = 7; } 3) Initialize and Use Variables typedef struct workerT //*****employee record ***** { char *name; // string constant float salary; // in dollars char ssn[11]; // xxx-xx-xxxx int ded; // tax deductions } workerT; int main() { workerT emp1,staff[30]; float tax; Use `dot' operator to access the `members' or `fields' of the data structure emp1.name = "Bob Cratchit"; emp1.salary = 10.00; strncpy(emp1.ssn,"022-85-7741",11); emp1.ded = 7; tax = (emp1.salary emp1.ded*EXEMPTION)*TAX_RATE; } ...
View Full Document

Ask a homework question - tutors are online