prior In Class Exercise ICE: Saving Pennies - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

prior In Class Exercise ICE: Saving Pennies

Description:

... of money you would have in a year if you saved 1 penny the first day, 2 more ... the second day (for a total of 3 cents), 3 more pennies the third day (for a ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 9
Provided by: need9
Category:
Tags: ice | class | day | exercise | pennies | prior | saving | third

less

Transcript and Presenter's Notes

Title: prior In Class Exercise ICE: Saving Pennies


1
prior In Class Exercise (ICE) Saving Pennies
  • Write a program that computes the amount of money
    you would have in a year if you saved 1 penny the
    first day, 2 more pennies the second day (for a
    total of 3 cents), 3 more pennies the third day
    (for a total of 6 cents), and so forth.
  • Compile and run your program using g
  • Update We will do this at the end of the period,
    if there is time.

2
C Pointer Review
  • Pointers
  • De-referencing operator
  • Address of operator
  • Indirection


3
PointerSpeak Reading C pointer syntax
  • declarations
  • double a a is a memory location that holds a
    floating point (holds garbage until told
    otherwise).
  •  double p p is a pointer to a double, can
    hold the memory address of a double (but hasnt
    yet been told to point to any particular address)
  • executable statements
  • p a p gets assigned the value of the address
    of a, therefore, p now points to the object a
  • p a2 object p points to (here, a) gets
    assigned 2 times the previous contents of a

4
Pointer Declarations
  • Pointers can point to any type (primitive
    datatypes or even user-defined classes)
  • int, double, char, Ship,
  • Pointers must be declared before being used
  • char letterPtr
  • letterPtr is potentially capable of pointing to a
    character.
  • double dblPtr1, dblPtr2, myQpr
  • two double pointers and 1 actual double

5
Pointer Initialization
  • Should be initialized (made to point somewhere)
    when declared
  • int myInt 5
  • int intPtr myInt // intPtr points to myInt
  • int intPtr 0
  • int intPtr NULL // equivalent statements
  • Accessing a pointer that does not point to a
    valid address results in a segmentation violation
  • Result a run time error that crashes your program

6
Indirection/Dereferencing Operators
  • The operator (dereference)
  • Dereferences the object the operand points to
  • Dereferences means that expression evaluates as
    the contents of the memory cell
  • Cant dereference non-pointers
  • The operator (address of)
  • Returns the address of the memory cell
  • double vBigNum 34.56
  • double myPtr vBigNum
  • //What prints out?
  • cout ltlt myPtr ltlt endl
  • //What happens here?
  • myPtr 78.90
  • //What is happening here?
  • cin gtgt myPtr

7
Some nitty gritty pointer operations
  • double vBigNum 34.56 // pointer
    demo1
  • double myPtr vBigNum
  • cout ltlt "\nThe Pointer " ltlt myPtr ltlt endl
  • cout ltlt "The variable " ltlt vBigNum ltlt endl
  • cout ltlt "\nThe address of the pointer " ltlt myPtr
    ltlt endl
  • cout ltlt "The address of the variable " ltlt
    vBigNum ltlt endl
  • cout ltlt "\nDerefing the address (myPtr) " ltlt
    myPtr ltlt endl
  • cout ltlt "and the addr of the deref (myPtr) " ltlt
    myPtr ltlt endl
  • myPtr 78.90
  • cout ltlt "\nPointer " ltlt myPtr ltlt " Variable " ltlt
    vBigNum ltlt endl

8
Pointers in function calls ptrdemo2.cpp
  • int callByVal (int) //call by value function
    prototype declaration
  • void callByRef (int ) //call by reference
    function prototype
  • int main ()
  • // g -c ptrdemo2.cpp
  • int myVar 1024 // g -o ptrdemo2
    ptrdemo2.o
  • int myPtr myVar
  • callByVal (myVar)
  • cout ltlt "After call by value function " ltlt
    myVar ltlt endl
  • callByRef (myPtr)
  • cout ltlt "After call by reference passing a ptr
    "ltltmyVar ltltendl
  • return 0 // what if you leave the function
    definitions out?

int callByVal (int number) number
number return number //end callByValue
  • void callByRef (int numPtr)
  • numPtr numPtr
  • //end callByRef

9
ICE Working With Pointers
  • Determine what the following C code produces
    at run time.
  • int a, p
  • a 8
  • p a
  • p 3 p - a
  • cout ltlt p
Write a Comment
User Comments (0)
About PowerShow.com