CS110_08b_structNesting

CS110_08b_structNesting - Structured Data Nesting 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: Structured Data Nesting Jack Tumblin jet@cs.northwestern.edu EECS110: 8b . Review: Why use Malloc() and Free()? for 'variable-size arrays' arrays To create/destroy 'objects': 'objects' words, lines, paragraphs, pages in a word processor notes, bars, pages in a music program, Columns, rows in a spreadsheet program, points, lines, and shapes in a drawing program customers in a banking system... (Recall)Dynamic Alloc. for Structs I Just as we can have fixed arrays of structures (e.g. workerT staff[30]; ) We can have dynamically allocated arrays of structs: int k,kmax; workerT *pWho; pWho = (workerT *)malloc(30*sizeof(workerT)); // now pWho points to 30 workerT elements... pWho[0].name =(char *)malloc(31*sizeof(char)); strncpy(pWho[0].name,"Ebenezer Scrooge",30); pWho[0].salary = 250.00; strncpy(pWho[0].ssn,"001-34-8902",11); pWho[0].ded = 1; pWho[1].name = "Bob Cratchit"; pWho[1].salary = 10.00; ... (Recall)Dynamic Alloc. for Structs II Data structures can have pointer members, too, so... typedef struct workerT { char *name; float salary; char ssn[11]; int ded; } workerT; // // // // string variable in dollars xxx-xx-xxxx tax deductions // ****employee record ****** workerT emp1; // We can allocate memory for struct MEMBERS! emp1.name =(char *)malloc(31*sizeof(char)); strncpy(emp1.name,"Ebenezer Scrooge",30); emp1.salary = 250.00; strncpy(emp1.ssn,"001-34-8902",11); emp1.ded = 1; emp1.name = "Bob Cratchit"; emp1.salary = 10.00; Grouping Data Together: 1 Two forms of arrays: Fixed arrays char msg[81]; Dynamically Alloc'd arrays Dynamic (pointer + malloc(), free() ...) char *msg; msg = (char *)malloc(81*sizeof(char)); ... free(msg); Grouping Data Together: 2 Several kinds of nested arrays, too: Multidimensional array (fixed size, fixed data type, quirky function syntax) char msgList[5][81]; Pointer-to-pointers: very treacherous syntax! char **msgList; msgList = (char **)malloc(5*sizeof(char *)); msgList[0] = (char *)malloc(81*sizeof(char)); msgList[1] = (char *)malloc(81*sizeof(char)); ... Mix them: could make arrays of pointers... Advanced Data Nesting Instruction nesting gave us few choices (recall: operator statement block of statemts. functions modules) Data nesting is much richer! we can mix and match: Ordinary variable types: char, int, float, double Groups of variables of the same type: fixed arrays dynamic arrays (pointers + malloc(), free()) Groups of variables of different types: Structures ANY nested combination of these! structures that hold arrays of pointers to structures that hold pointers to arrays of... Data Nesting: Many choices! Basic data types: int, char, float, double Derived data types: arrays, *pointers, structures Combine to make rich, complex data collections: Best ways? HINT: nested structs keep it simple! derived type basic type array of.... pointer to... (dyn. alloc'd) int char float double struct with... Data Nesting: Examples Recall: multidimensional arrays Array of Arrays of char: char msgList[10][81] = { {"Press zero now"}, {"Press one now"}, ... } derived type basic type array of.... pointer to... (dyn. alloc'd) int char float double struct with... Simple, but rearranging Recall: multidimensional arrays messages is difficult. And which index is message#, Array of Arrays of char: which is char#? char msgList[10][81] = { {"Press zero now"}, {"Press one now"}, ... } derived type basic type Data Nesting: Examples array of.... pointer to... (dyn. alloc'd) int char float double struct with... Data Nesting: Examples Array of Pointers to array of char(string): char *msgList[10]; // array of pointers char msg0[81] = {"Press zero now"}; // char strings char msg1[81] = {"Press one now"}; char msg2[81] = {"Press two now"}; ... ... // Point to the strings: msgList[0] = msg0; msgList[1] = msg1; ... printf("%s", msgList[k]); // print k-th message derived type basic type array of.... pointer to... (dyn. alloc'd) int char float double struct with... Data Nesting: Examples Array of Pointers to array of char(string): char *msgList[10]; // array of pointers Only one char selects char msg0[81] = {"Press zero now"}; // index; stringsthe message#. Also: easy to char msg1[81] = {"Press one now"}; char msg2[81] = {"Press two now"}; rearrange messages ... (swap the pointers), but ... // Point to the strings: tedious to program, and msgList[0] = msg0; msgList[1] = msg1; ... printf("%s", msgList[k]); // print k-th message derived type message size is fixed. basic type int char float double array of.... pointer to... (dyn. alloc'd) struct with... Data Nesting: Examples Array of Pointers to char(string fixed array) char *msgList[10]; // array of 10 char pointers int i; for(i=0; i<10; i++) // get memory for each ptr { msgList[i] = (char *)malloc(81*sizeof(char)); } // put string at each ptr strncpy(msgList[0],"Press zero now",80); strncpy(msgList[1],"Press one now",80); derived type basic type array of.... pointer to... (dyn. alloc'd) int char float double struct with... Data Nesting: Examples Array of Pointers to char(string fixed array) char *msgList[10]; // array of 10 char pointers Message size can change int i; at runtime, but the for(i=0; i<10; i++) // get memory for each ptr { number of messages is msgList[i] = (char *)malloc(81*sizeof(char)); still fixed at compile time. } // put string at each ptr strncpy(msgList[0],"Press zero now",80); strncpy(msgList[1],"Press one now",80); derived type basic type array of.... pointer to... (dyn. alloc'd) int char float double struct with... Data Nesting: Examples Pointer to (dyn. alloc'd) (dyn. alloc'd) char **msgList; // pointer-to-pointer-to-char // aim at nK pointers-to-char msgList = (char **)malloc( nK * sizeof(char *)); for(i=0; i<nK; i++) // make 10 char buffers { msgList[i] =(char *)malloc(81*sizeof(char)); } // put string at each ptr strncpy(msgList[0],"Press zero now",80); strncpy(msgList[1],"Press one now",80); derived type basic type pointers to chars array of.... pointer to... (dyn. alloc'd) int char float double struct with... Data Nesting: Examples Pointer to (dyn. alloc'd) (dyn. alloc'd) char **msgList; // pointer-to-pointer-to-char // aim at nK pointers-to-char msgList = (char **)malloc( nK * sizeof(char *)); for(i=0; i<nK; i++) // make 10 char buffers { Can change # of msgs, msgList[i] =(char *)malloc(81*sizeof(char)); } // put string of msgs, order length at each ptr strncpy(msgList[0],"Press of messages. BUT zero now",80); strncpy(msgList[1],"Press one now",80); derived type basic type pointers to chars ugly, confusing syntax... int char float double array of.... pointer to... (dyn. alloc'd) struct with... Data Nesting: Examples Array of structs with Array [0] `string' struct [1] `string' struct [2] `string' struct ... derived type basic type (dyn. alloc'd) pointers to chars array of.... pointer to... (dyn. alloc'd) int char float double struct with... Data Nesting: Examples Array of structs with typedef struct stringT { char *pTxt; // dyn. alloc'd char buffer int size; // max# of chars allowed in buffer } stringT; int main(void) { stringT msgList[10]; int k; (dyn. alloc'd)pointers to chars // Define a `message' struct Define a structure that holds everything we need for a `string' for(k=0; k<10; k++) { msgList[k].size = 81; msgList[k].pTxt = // get memory for string k (char *)malloc(msgList[k].size*sizeof(char)); } strncpy(msgList[0].pTxt,"Press zero now",80); ... Data Nesting: Examples Array of structs with typedef struct stringT { char *pTxt; // dyn. alloc'd char buffer int size; // max# of chars allowed in buffer } stringT; int main(void) { stringT msgList[10]; int k; (dyn. alloc'd)pointers to chars // Define a `message' struct Make an array of `string' objects, for(k=0; k<10; k++) { msgList[k].size = 81; msgList[k].pTxt = // get memory for string k (char *)malloc(msgList[k].size*sizeof(char)); } strncpy(msgList[0].pTxt,"Press zero now",80); ... Data Nesting: Examples Array of structs with typedef struct stringT { char *pTxt; // dyn. alloc'd char buffer int size; // max# of chars allowed in buffer } stringT; int main(void) { stringT msgList[10]; int k; (dyn. alloc'd)pointers to chars // Define a `message' struct Make an array of `string' objects, set their buffer sizes, and fill with messages for(k=0; k<10; k++) { msgList[k].size = 81; msgList[k].pTxt = // get memory for string k (char *)malloc(msgList[k].size*sizeof(char)); } strncpy(msgList[0].pTxt,"Press zero now",80); ... Data Nesting: Examples Array of structs with typedef struct stringT { char *pTxt; // dyn. alloc'd char buffer int size; // max# of chars allowed in buffer } stringT; int main(void) { stringT msgList[10]; int k; (dyn. alloc'd)pointers to chars // Define a `message' struct Make an array of `string' objects, set their buffer sizes, and fill with messages for(k=0; k<10; k++) { msgList[k].size = 81; msgList[k].pTxt = // get memory for string k (char *)malloc(msgList[k].size*sizeof(char)); } strncpy(msgList[0].pTxt,"Press zero now",80); ... Example: You try it! Dynamic Array of structs with (dyn. alloc'd)pointers to chars typedef struct stringT // Define a `message' struct { char *pTxt; // dyn. alloc'd char buffer */ array Make an dynamic int size; // max# of chars allowed in buffer of 10 `string' objects, } stringT; int main(void) and fill with messages { stringT *pMsgList; // dyn. alloc'd array of strings int k; //???? What would you do to allocate the pMsgListArray ???? for(k=0; k<10; k++) { pMsgList[k].size = 81; pMsgList[k].pTxt = // get memory for string k (char *)malloc(pMsgList[k].size*sizeof(char)); } strncpy(pMsgList[0].pTxt,"Press zero now",80); ... set their buffer sizes, Data Nesting ANY combination is possible, possible but only a few are simple, sensible, and easy to read. PLAN your data scheme; What is the simplest, easiest-to-read way to arrange all the data in your program? Organize, nest, group together related variables to simplify: structs are your best, most flexible tool derived type basic type array of.... pointer to... (dyn. alloc'd) int char float double struct with... ...
View Full Document

Ask a homework question - tutors are online