I hope you: - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

I hope you:

Description:

I hope you: Are much happier with PA-3(revised), Due Today. ... emp1.name = 'Bob Cratchit'; emp1.salary = 10.00; Pass by Reference' ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 30
Provided by: jack195
Category:
Tags: bob | hope

less

Transcript and Presenter's Notes

Title: I hope you:


1
CS110 Lecture 21 Structures With Arrays and
Pointers
Jack Tumblinjet_at_cs.northwestern.edu
  • I hope you
  • Are much happier with PA-3(revised), Due Today.
  • Are 'almost done' with PA-4 (short due Wed.
    11/19)
  • Are trying tiny practice programs using pointers
  • Are reading all about structures Chap. 12 (no
    12-8)

2
Referencing and De-referencing
  • Recall
  • referencing get address operator
  • int k2
  • pInt, arr35,6,7
  • pInt k / referencing / arr2 8 /
    array indexing / pInt0 1 / pointer
    indexing / pInt 1 / pointer
    de-referencing /
  • Dereferencing is same as indexing at zero!
  • Notation VERY confusinglooks like pointer
    declaration!
  • ( pInt pInt0 ) / always true /

3
Referencing and De-referencing
  • Recall
  • indexing get element operator
  • int k2
  • pInt, arr35,6,7
  • pInt k / referencing / arr2 8 /
    array indexing / pInt0 1 / pointer
    indexing / pInt 1 / pointer
    de-referencing /
  • Dereferencing is same as indexing at zero!
  • Notation VERY confusinglooks like pointer
    declaration!
  • ( pInt pInt0 ) / always true /

4
Referencing and De-referencing
  • Recall (Book does this too soon!)
  • de-referencing value pointed to op
  • int k2
  • pInt, arr35,6,7
  • pInt k / referencing / arr2 8 /
    array indexing / pInt0 1 / pointer
    indexing / pInt 1 / pointer
    de-referencing /
  • Dereferencing is same as indexing at zero!
  • Notation VERY confusinglooks like pointer
    declaration!
  • ( pInt pInt0 ) / always true /

5
Dynamic Allocation Example
malloc() and free() are defined in stdlib.h
  • include ltstdlib.hgt
  • int main(void)
  • int k,kmax,pKey,pDone...pKey (int
    )malloc( kmaxsizeof(int) )for(k0 kltkmax
    k) pKeyk rand()... (more work
    with pKey) ...
  • pDone pKey
  • free(pDone)

malloc(n) function reserve a block of n
bytes (Block has no name)
free(ptr) function release a block of bytes at
address ptr.
6
CS110 Lecture 21 Structures With Arrays and
Pointers
Jack Tumblinjet_at_cs.northwestern.edu
  • I hope you
  • Are much happier with PA-3(revised), Due Today.
  • Are 'almost done' with PA-4 (short due Wed.
    11/19)
  • Are trying tiny practice programs using pointers
  • Are reading all about structures Chap. 12 (no
    12-8)

7
(Recall) Enumerated Types
  • Define a new enum data type, Declare variables
    of it
  • typedef enum / define a new data type /
    Sun, Mon, Tues, Wed, Thu, Fri, Sat
  • weekdayT
  • int main()
  • weekdayT today,tomorrow / declare vars /
  • today Sat
  • tomorrow (today-1)7 /computed as int/...

Name of our new data type
Declare some variables of type weekdayT
8
(Recall) Enumerated Types
  • Mostly a notational convenience
  • Works just like define statements
  • typedef enum
  • Sun, Mon, Tues, Wed, Thu, Fri, Sat
  • weekdayT
  • enum variables given int values internally, or
    you can assign them explicitly (see book)
  • No restrictions on value--today 578 allowed
    (but meaningless)

0 1 2 3 4 5 6
9
1) Define a Data Structure
  • typedef struct workerT /employee record
    /
  • char name / string variable /float
    salary / in dollars /char ssn11 /
    xxx-xx-xxxx /int ded / tax deductions /
  • workerT
  • int main()
  • workerT emp1,staff30
  • emp1.name Bob Cratchitemp1.salary
    10.00strncpy(emp1.ssn,022-85-7741,11)emp1.de
    d 7

define a new type for me
it is a data structure, and
The name of the new data type is
The members of the new data type are
10
2) Declare Variables of the new Type
  • typedef struct workerT /employee record
    /
  • char name / string variable /float
    salary / in dollars /char ssn11 /
    xxx-xx-xxxx /int ded / tax deductions /
  • workerT
  • int main()
  • workerT emp1, pGood, staff30int i,imax
  • emp1.name Bob Cratchitemp1.salary
    10.00strncpy(emp1.ssn,022-85-7741,11)emp1.de
    d 7

workerT is now a data type. Declare an
ordinary variable emp1, and a pointer-to-workerT
variable pGood, and an array of workerT elements
staff30
11
3) Initialize and Use struct Variables
  • typedef struct workerT /employee record
    /
  • char name / string variable /float
    salary / in dollars /char ssn11 /
    xxx-xx-xxxx /int ded / tax deductions /
  • workerT
  • int main()
  • workerT emp1,staff30int i,imax
  • emp1.name Bob Cratchitemp1.salary
    10.00strncpy(emp1.ssn,022-85-7741,11)emp1.de
    d 7

Use dot operator to access the members or
fields of the data structure
12
A Beginners Mistake
  • Dont confuse data type you defined with a data
    structure and the variables of that type

typedef struct workerT / employee record
/ char name / string variable
/ float salary / in dollars / char
ssn11 / xxx-xx-xxxx / int ded / tax
deductions / workerT int main() workerT
emp1,staff30int i,imax workerT.name
Bob Cratchit workerT.salary 10.00 ...
Compiler Errors!! ?What's wrong?
13
A Beginners Mistake
  • Dont confuse data type you defined with a data
    structure and the variables of that type

typedef struct workerT / employee record
/ char name / string variable
/ float salary / in dollars / char
ssn11 / xxx-xx-xxxx / int ded / tax
deductions / workerT int main() workerT
emp1,staff30int i,imax workerT.name
Bob Cratchit workerT.salary 10.00 ...
Data type!
ErrorUndeclared Variable?!?
14
A Beginners Mistake
  • Dont confuse data type you defined with a data
    structure and the variables of that type

typedef struct workerT / employee record
/ char name / string variable
/ float salary / in dollars / char
ssn11 / xxx-xx-xxxx / int ded / tax
deductions / workerT int main() workerT
emp1,staff30int i,imax emp1.name Bob
Cratchit emp1.salary 10.00 ...
Data type
Variable names
Correct way Variables of type 'workerT'
15
Operators Data Structures
  • For the basic data types, many operators work,
    , -, , /, lt, gt, lt, , gt, !, . etc.
  • Data structure types ONLY assignment () works
  • () is a Member-by-Member / Field-by-field copy
  • WILL copy char, int, float, double, enum values,
  • WILL copy array contents (because its size is
    fixed),
  • WILL copy a pointer, but
  • WONT copy strings or other data located at
    pointers

16
Operators Data Structures
  • typedef struct workerT / employee record
    /
  • char name / string variable /float
    salary / in dollars /char ssn11 /
    xxx-xx-xxxx /int ded / tax deductions /
  • workerT
  • int main()workerT staff30, emp1 / copy
    Scrooges info/ staff0.name Ebenezer
    Scrooge staff0.salary 250.00
    strncpy(staff0.ssn,001-34-8902,11)
    staff0.ded 1 emp1 staff0
    printf(s, 5.2f, ssns, dedd\n,
    emp1.name,emp1.salary,emp1.ssn, emp1.ded)
  • RESULT gt Ebenezer Scrooge, 250.00,
    ssn001-34-8902, ded1 gt

17
Arrays of Data Structures
  • Data Structure a named group of variables
  • workerT emp1... emp1.name Bob
    Cratchit emp1.salary 10.00 emp1.ssn
    022-85-774 emp1.ded 7 etc. ...

Data structure members
Array element
staff0 emp1 staff2 staff3 ...
18
Arrays of Data Structures
  • Data Structure a named group of variables
  • How can we describe lots of complicated
    'objects', where each 'object' has SEVERAL
    variable types? EXAMPLE employee ledger
    moving objects in a videogame...
  • Answer make an array of Data Structures

staff0 staff1 staff2 staff3 ...
19
Arrays of Data Structures
  • Example
  • float costint k,kmaxworkerT staff30
    staff0.name Ebenezer Scrooge
    staff0.salary 250.00 strncpy(staff0.ssn
    ,001-34-8902,11) staff0.ded 1
    staff1.name Bob Cratchit
    staff1.salary 10.00 . . .

typedef struct workerT / employee record
/ char name / string variable
/ float salary / in dollars / char
ssn11 / xxx-xx-xxxx / int ded / tax
deductions / workerT
20
Pointers-to-Struct
  • Pointers work on structs exactly as before...
  • workerT staff30, emp1 / struct variables
    /
  • workerT pGood / pointer-to-struct /
  • ...
  • pGood emp1 / point to 'emp1' /
  • pGood staff 3 / point to staff3
    /
  • Use indirection ( or 0) to get
    members pGood0.salary 1000 / Give a
    raise / / OR you can write
    / (pGood).salary 1000 / Give a raise
    /
  • Operator precedence for makes a mess
    (ANOTHER reason to hate the form of
    de-referencing)
  • pGood.salary 1000

!NO! -- !ERROR!
21
Pointers-to-Struct -gt
  • Pointers work on structs exactly as before...
  • workerT staff30, emp1 / struct variables
    /
  • workerT pGood / pointer-to-struct /
  • ...
  • pGood emp1 / point to 'emp1' /
  • pGood0.salary 1000 / Give a raise
    /
  • pGood staff 3 / point to staff3 /
  • (pGood).salary 1000 / Give another
    raise /
  • Operator precedence for makes a mess!
    (ANOTHER reason to hate the form of
    de-referencing)
  • pGood.salary 1000
  • INSTEAD use this nearly-pictorial operator
    pGood-gtsalary 1000

SAME
!NO! --!ERROR!
22
Dynamic Alloc. for Structures I
  • 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 ...

23
Dynamic Alloc. for Structures II
  • Data structures can have pointer members, too...

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
24
Pass by Reference
  • Function arguments that are pointers
  • Copy an address to the formal parameters,
  • But changing formal parameter (the address) in
    the function NEVER changes the actual argument!

formal parameters
main (void) double val1.234double pd
pd val dfixit(pd) / now val42.0
/ / pd unchanged /
actual argument
void dfixit2(double x) x0 42.0
x / move!? /
NO!
25
Functions and Data Structures
  • What if function arguments are 'struct'-type
    variables?
  • works just fine copies struct to formal
    parameters...
  • workerT give_raise(workerT b4) /costly
    prototype! /
  • int main(void)
  • workerT emp1
  • ...
  • emp1 give_raise( emp1 ) /costly fcn
    call /
  • ...
  • workerT give_raise(workerT b4) / costly
    fcn body /
  • b4.salary 1000.00 ...
    return(b4)

COPY
26
Functions and Data Structures
  • Recall that we call a function,we copy actual
    arguments to formal parameters
  • workerT give_raise(workerT b4) /costly
    prototype! /
  • int main(void)
  • workerT emp1
  • ...
  • emp1 give_raise( emp1 ) /costly fcn
    call /
  • ...
  • workerT give_raise(workerT b4) / costly
    fcn body /
  • b4.salary 1000.00 ...
    return(b4)

COMPUTE
27
Functions and Data Structures
  • And when return something from a function,we
    copy it from local variables...
  • workerT give_raise(workerT b4) /costly
    prototype! /
  • int main(void)
  • workerT emp1
  • ...
  • emp1 give_raise( emp1 ) /costly fcn
    call /
  • ...
  • workerT give_raise(workerT b4) / costly
    fcn body /
  • b4.salary 1000.00 ...
    return(b4)

COPY
28
Functions and Data Structures
  • Structs are just one more data type it all works
    fine.
  • BUT data structures may be HUGE!
    (suppose the workerT struct includes
    a 500KB photo)do you need to copy back and forth
    to/from functions?
  • workerT give_raise(workerT b4) /costly
    prototype! /
  • int main(void)
  • workerT emp1
  • ...
  • emp1 give_raise( emp1 ) /costly
    fcn call /

COPY THE BIGINPUT STRUCT to b4
COPY THE BIGRETURNED STRUCT to emp1
29
Functions and Data Structures
  • Make a habit of Pass-by-Reference for structures
  • Copies only the address to functionFAST!
  • No return value neededEfficient!
  • void give_raise(workerT pGood) / efficient
    prototype!/
  • int main(void)
  • workerT emp1
  • ...
  • give_raise(emp1) / Better! use a
    pointer / ...
  • void give_raise(workerT pGood) / efficient
    function body/
  • pGood-gtsalary 1000.0 / Note the
    'shorthand' / / (same as
    (pGood0).salary or (pGood).salary /
Write a Comment
User Comments (0)
About PowerShow.com