Title: I hope you:
1CS110 Lecture 22 Structured Data Nesting
Jack Tumblinjet_at_cs.northwestern.edu
- I hope you
- Have finished PA4, Due Wed 11/19
- Have read Chap. 12 (skip 12-8)
- Will look at PA-5 ( --similar to PA-3!-- )
2NEW! Blackboard turn-in
- For project 4 and 5, use 'Digital Drop Box'
Blackboard Tools button, Digital
Drop Box, Add File, then Send File - Be sure to SEND, don't just ADD FILE We don't
receive your file until you hit 'send'! - No need to turn in paper printouts
3Review Why use Malloc() and Free()?
- for 'variable-size arrays'
- To create/destroy 'objects'
- words in a word processor
- notes in a music program,
- Columns, rows in a spreadsheet program,
- points, lines, and shapes in a drawing program
- customers in a banking system...
4(Recall)Dynamic Alloc. for Structs I
- Data structures can have pointer members, too,
so...
typedef struct workerT / employee record
/ char name / string variable
/ float salary / in dollars / char
ssn11 / xxx-xx-xxxx / int ded / tax
deductions / workerT
workerT emp1 emp1.name (char
)malloc(31sizeof(char)) strncpy(emp1.name,Eb
enezer Scrooge,30) emp1.salary 250.00
strncpy(emp1.ssn,001-34-8902,11) emp1.ded
1 emp1.name Bob Cratchit emp1.salary
10.00
5(Recall)Dynamic Alloc. for Structs II
- Just as we can have fixed arrays of
structures(e.g. workerT staff30 ) - We can have dynamically allocated arrays of
structs - int k,kmaxworkerT pWho
- pWho (workerT )malloc(30sizeof(worker
T)) / now pWho points to 30 workerT
elements.../ - pWho0.name (char )malloc(31sizeof(char))
strncpy(pWho0.name,Ebenezer Scrooge,30)
pWho0.salary 250.00 strncpy(pWho0.ssn,00
1-34-8902,11) pWho0.ded 1 pWho1.name
Bob Cratchit pWho1.salary 10.00 ...
6Grouping Data Together
- Two forms of arrays
- Fixed arrays char msg81
- Dynamically Allocd arrays (pointer
malloc(), free() ...)char msg msg
(char )malloc(81sizeof(char)) ...
free(msg)
7Grouping Data Together
- Several kinds of nested arrays, too
- Multidimensional array (fixed size, fixed data
type, quirky function syntax)char
msgList581 - Pointer-to-pointers very treacherous syntax!
- char msgList
- msgList (char )malloc(5sizeof(char
))msgList0 (char )malloc(81sizeof(char))
... - Mixed arrays of pointers...
8Advanced Data Nesting
- Instruction nesting gave us few choices (recall
operator?statement?block of statemts.?functions?mo
dules) - 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...
9Data Nesting Many choices!
- Basic data types int, char, float, double
- Derived data types arrays, pointers,
structures - Combine to make endlessly complex collections
basic type
derived type
intcharfloat double
array of.... pointer to...
(dyn. allocd) struct with...
10Data Nesting Examples
- Recall multidimensional arraysArray of ?Arrays
of?char - char msgList1081 Press zero
now, Press one now, ...
basic type
derived type
intcharfloat double
array of.... pointer to...
(dyn. allocd) struct with...
11Data Nesting Examples
Simple, but rearranging messages is difficult.
And which index is message, which is char?
- Recall multidimensional arraysArray of ?Arrays
of?char - char msgList1081 Press zero
now, Press one now, ...
basic type
derived type
intcharfloat double
array of.... pointer to...
(dyn. allocd) struct with...
12Data Nesting Examples
- Array of ?Pointers to ?char(string)char
msgList10 / array of pointers
/char msg081 Press zero now / char
strings/char msg181 Press one nowchar
msg281 Press two now......
/Point to the strings / - msgList0 msg0 msgList1 msg1
...printf(s, msgListk) / print k-th
message /
basic type
derived type
intcharfloat double
array of.... pointer to...
(dyn. allocd) struct with...
13Data Nesting Examples
- Array of ?Pointers to ?char(string)char
msgList10 / array of pointers /char
msg081 Press zero now / char
strings/char msg181 Press one nowchar
msg281 Press two now......
/Point to the strings / - msgList0 msg0 msgList1 msg1
...printf(s, msgListk) / print k-th
message /
Only one index selects the message. Also easy
to rearrange messages (swap the pointers),
but tedious to program, and message size is fixed.
basic type
derived type
intcharfloat double
array of.... pointer to...
(dyn. allocd) struct with...
14Data Nesting Examples
- Array of ?Pointers to ?char(string)char
msgList10 / array of 10 char pointers /int
i - for(i0 ilt10 i) / get memory for each ptr
/ msgListi (char )malloc(81sizeof(c
har)) / put string at each
ptr / strncpy(msgList0,Press zero now,80)
strncpy(msgList1,Press one now,80)
basic type
derived type
intcharfloat double
array of.... pointer to...
(dyn. allocd) struct with...
15Data Nesting Examples
- Array of ?Pointers to ?char(string)char
msgList10 / array of 10 char pointers /int
i - for(i0 ilt10 i) / get memory for each ptr
/ msgListi (char )malloc(81sizeof(c
har)) / put string at each
ptr / strncpy(msgList0,Press zero now,80)
strncpy(msgList1,Press one now,80)
Message size can change at runtime, but the
number of messages is still fixed at compile
time.
basic type
derived type
intcharfloat double
array of.... pointer to...
(dyn. allocd) struct with...
16Data Nesting Examples
- Pointer to ? (dyn. allocd)pointers to ? (dyn.
allocd)chars - char msgList / pointer-to-pointer-to-char
/ / aim at nK pointers-to-char / msgList
(char )malloc( nK sizeof(char )) - for(i0 iltnK i) / make 10 char buffers
/ msgListi (char )malloc(81sizeof(ch
ar)) / put string at each ptr
/ strncpy(msgList0,Press zero now,80)
strncpy(msgList1,Press one now,80)
basic type
derived type
intcharfloat double
array of.... pointer to...
(dyn. allocd) struct with...
17Data Nesting Examples
- Pointer to ? (dyn. allocd)pointers to ? (dyn.
allocd)chars - char msgList / pointer-to-pointer-to-char
/ / aim at nK pointers-to-char / msgList
(char )malloc( nK sizeof(char )) - for(i0 iltnK i) / make 10 char buffers
/ msgListi (char )malloc(81sizeof(ch
ar)) / put string at each ptr
/ strncpy(msgList0,Press zero now,80)
strncpy(msgList1,Press one now,80)
Can change of msgs, length of msgs, order of
messages. BUT ugly, confusing syntax...
basic type
derived type
intcharfloat double
array of.... pointer to...
(dyn. allocd) struct with...
18Data Nesting Examples
- Array of?structs with? (dyn. allocd)pointers
to?chars
Array
0 1 2 ...
basic type
derived type
intcharfloat double
array of.... pointer to...
(dyn. allocd) struct with...
19Data Nesting Examples
- Array of?structs with? (dyn. allocd)pointers
to?chars - typedef struct stringT / Define a message
struct / - char pTxt / dyn. allocd char buffer
/int size / max of chars allowed in
buffer / - stringT
- int main(void)
- stringT msgList10 int kfor(k0 klt10
k) msgListk.size 81 msgListk.pTxt
/ get some memor (char )malloc(msgListk.s
izesizeof(char)) -
- strncpy(msgList0.pTxt,Press zero now,80)
...
Define a structure that holds everything we need
for a string
20Data Nesting Examples
- Array of?structs with? (dyn. allocd)pointers
to?chars - typedef struct stringT / Define a message
struct / - char pTxt / dyn. allocd char buffer
/int size / max of chars allowed in
buffer / - stringT
- int main(void)
- stringT msgList10 int kfor(k0 klt10
k) msgListk.size 81 msgListk.pTxt
/ get memory for string k / (char
)malloc(msgListk.sizesizeof(char)) -
- strncpy(msgList0.pTxt,Press zero now,80)
...
Make an array of string objects,
21Data Nesting Examples
- Array of?structs with? (dyn. allocd)pointers
to?chars - typedef struct stringT / Define a message
struct / - char pTxt / dyn. allocd char buffer
/int size / max of chars allowed in
buffer / - stringT
- int main(void)
- stringT msgList10 int kfor(k0 klt10
k) msgListk.size 81 msgListk.pTxt
/ get memory for string k / (char
)malloc(msgListk.sizesizeof(char)) -
- strncpy(msgList0.pTxt,Press zero now,80)
...
Make an array of string objects, set their
buffer sizes, and fill with messages
22Data Nesting Examples
- Array of?structs with? (dyn. allocd)pointers
to?chars - typedef struct stringT / Define a message
struct / - char pTxt / dyn. allocd char buffer
/int size / max of chars allowed in
buffer / - stringT
- int main(void)
- stringT msgList10 int kfor(k0 klt10
k) msgListk.size 81 msgListk.pTxt
/ get memory for string k / (char
)malloc(msgListk.sizesizeof(char)) -
- strncpy(msgList0.pTxt,Press zero now,80)
...
Make an array of string objects, set their
buffer sizes, and fill with messages
23Example You try it!
- Dynamic Array of?structs with? (dyn.
allocd)pointers to?chars - typedef struct stringT / Define a message
struct / - char pTxt / dyn. allocd char buffer
/int size / max of chars allowed in
buffer / - stringT
- int main(void)
- stringT pMsgList / dyn. alloc'd array of
strings / int k???? What would you do to
allocate the pMsgListArray ????for(k0 klt10
k) pMsgListk.size 81 pMsgListk.pTx
t / get memory for string k / (char
)malloc(pMsgListk.sizesizeof(char)) -
- strncpy(pMsgList0.pTxt,Press zero now,80)
...
Make an dynamic array of 10 string
objects, set their buffer sizes, and fill with
messages
24Data Nesting
- ANY combination is 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
basic type
derived type
intcharfloat double
array of.... pointer to...
(dyn. allocd) struct with...