Chapter 9 Pointers Getting the address of a Variable - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Chapter 9 Pointers Getting the address of a Variable

Description:

address and the sizeof operator to determine its size. sizeof(type) is also okay ... cout 'The size of X is ' sizeof(X) ' bytesn' ... – PowerPoint PPT presentation

Number of Views:91
Avg rating:3.0/5.0
Slides: 31
Provided by: cathe100
Category:

less

Transcript and Presenter's Notes

Title: Chapter 9 Pointers Getting the address of a Variable


1
Chapter 9 Pointers Getting the address of a
Variable
  • Why do we have pointers?
  • Indirection difference between
  • Will you go out with me?
  • Will you go out with person whose name is on
    cafeteria door?
  • The address operator () returns the memory
    address of a variable.

2
Addresses for our machineschar 1int 4, short
int 2float 4
3
Use of Operator Program
  • // This program uses the operator to determine
    a variables
  • // address and the sizeof operator to determine
    its size.
  • sizeof(type) is also okay
  • sizeof(array) gives length of types number in
    array
  • sizeof(pointer) gives length of pointer itself (4
    for us)
  • include ltiostream.hgt
  • void main ( void )
  • int arr10
  • int X 25
  • int a new int10
  • cout ltlt "The address of X is " ltlt X ltlt endl
  • cout ltlt "The size of X is " ltlt sizeof(X) ltlt "
    bytes\n"
  • cout ltlt "The value in X is " ltlt X ltlt endl
  • // main

4
Pointer Variables
  • Pointer variables, which are often just called
    pointers, are designed to hold memory addresses.
  • With pointer variables you can indirectly
    manipulate data stored in other variables.

5
Pointer Variables Program
  • // This program stores the address of a variable
    in a pointer.
  • include ltiostream.hgt
  • void main ( void )
  • int X 25
  • int Ptr //I like to read this backwards
  • Ptr X // Store the address of X in Ptr
  • cout ltlt "The value in X is " ltlt X ltlt endl
  • cout ltlt "The address of X is " ltlt Ptr ltlt endl
  • // main
  • Note and are overloaded operators you have
    seen them for other uses.

6
Draw pictures to help you understandint num1,
num210 float f12.5 float fptr int
iptr iptr num2 iptr 50 fptr
f1 num1 num2 iptr fptr What is the
value of num1?

7
Pointer Variables
What about taking the address of an address? int
addP addP Ptr Only works if address is
actually stored somewhere
8
Indirection Operator Program
  • // This program demonstrates the use of the
    indirection
  • // operator.
  • include ltiostream.hgt
  • void main ( void )
  • int X 25
  • int Ptr
  • Ptr X // Store the address of X in Ptr
  • cout ltlt "Here is the value in X, printed
    twice\n"
  • cout ltlt X ltlt " " ltlt Ptr ltlt endl
  • Ptr 100 // Note indirection is a unary
    operator
  • cout ltlt "Once again, here is the value in X\n"
  • cout ltlt X ltlt " " ltlt Ptr ltlt endl
  • // main

9
  • Note int x,y is the same as
  • int x
  • int y

10
At Seats
  • Write the code to read in an integer, access it
    via a pointer, add five to it, and then print it
    out.
  • Why are we using pointers? No real purpose here
    just to show we can and to gain experience.

11
Relationship Between Arrays and Pointers
  • Array names can be used as pointers, and
    vice-versa.
  • This is important in understanding how arrays are
    passed.
  • This is also important in allocating dynamic
    arrays.

12
  • Write a function
  • void clear_array(int array, int size)
  • which sets each element of the array to zero. 
  • Try it three different ways (two with pointers)

13
Here is the simplest way
  • void clear(int a, int size)
  • for (int i 0i lt size i)
  • ai 0

14
  • void clear(int a, int size)
  • int p
  • for (int pap lt asize p)
  • p 0

15
  • void clear(int a, int size)
  • for (int i 0i lt size i)
  • (ai) 0

16
At seats
  • Write a function int replace_char(char str, char
    old_char, char new_char) which replaces each
    occurrence of the character old_char with the
    character new_char in the string str.  The
    function should return the number of characters
    actually replaced. 

17
Repeat the previous exercise accessing the array
via pointers
18
Arrays and Pointers Program
  • include ltiostream.hgt
  • void main ( void )
  • short Numbers 10, 20, 30, 40, 50
  • // short Numbers would be similar, but no
    space allocated
  • // an array of short and a pointer to short
    are the
  • // SAME type to the compiler
  • cout ltlt "The first Oth element of the array is
    "
  • cout ltlt Numbers ltlt endl
  • cout ltlt "The third element of the array is "
  • cout ltlt (Numbers 2) ltlt endl
  • // Note, smart enough to add not just 2 but 2
    times
  • // the length of the type pointed to!
  • // main

19
Using Pointer Arithmetic
20
Pointers and Arrays Program
  • include ltiostream.hgt
  • void main ( void )
  • int Numbers5
  • cout ltlt "Enter five numbers "
  • for ( int Count 0 Count lt 5 Count )
  • cin gtgt (Numbers Count)
  • cout ltlt "Here are the numbers you entered\n"
  • for ( int Count 0 Count lt 5 Count )
  • cout ltlt (Numbers Count)ltlt " "
  • cout ltlt endl
  • // main

21
Consider these two swaps.What happens when you
pass two pointers to each?
  • void swap(int p, intq)
  • int t p
  • p q
  • q t
  • void swap2( int p, int q)
  • int t new int()
  • t p
  • p q
  • q t

22
Dynamic Arrays Program
  • float Sales
  • int NumDays
  • Sales new floatNumDays
  • delete Sales
  • float Sales210 // Sales2 is a constant
    pointer

23
At seats
  • Define an int pointer variable a. Then
  • (1) Use new to make a point to a dynamic array of
    5 cells of type int.
  • (2) Write a loop to fill a with values 3, 7, 11,
    15, 19.

24
At seats
  • Write code to read in the size of an array from
    input.
  • Allocate an array of strings of that size
  • Print them out backwards (last string first)
    using pointer variables (rather than subscripts)

25
Try this
  • Assume int x5 0, 1, 2, 3, 4. What does
    print3(x0) print? print3(x2)?
    print3(x4)?

void print3(int x) int i
for (i 0 i lt 3 i) cout
ltlt xi ltlt " " cout ltlt endl
26
Pointer Arithmetic
  • Some mathematical operations may be performed on
    pointers.
  • The and -- operators may be used to increment
    or decrement a pointer variable.
  • An integer may be added to or subtracted from a
    pointer variable. This may be performed with the
    , -, , or - operators.
  • A pointer may be subtracted from another pointer.
    (gives number of items between them not physical
    length)

27
Pointer Arithmetic Program
  • include ltiostream.hgt
  • void main ( void )
  • int Set8 5, 10, 15, 20, 25, 30, 35, 40
  • int Nums, Index
  • Nums Set
  • cout ltlt "The numbers in Set are\n"
  • for ( Index 0 Index lt 8 Index )
  • cout ltlt Nums ltlt " "
  • Nums
  • // for
  • cout ltlt "\nThe numbers in Set backwards are\n"
  • for ( Index 0 Index lt 8 Index )
  • Nums--
  • cout ltlt Nums ltlt " "
  • // for
  • // main

28
Comparing Pointers
  • If one address comes before another address in
    memory, the first address is considered less
    than the second.
  • Cs relational operators maybe used to compare
    pointer values.

29
Pointers as Function Parameters
  • A pointer can be used as a function parameter.
  • It gives the function access to the original
    argument, much like a reference parameter does.

30
Dynamic Memory Allocation
  • Variables may be created and destroyed while a
    program is running. This is help for linked
    lists.
  • A pointer than contains the address 0 is called a
    null pointer.
  • Use the new operator to dynamically allocate
    memory.
  • Use delete to dynamically deallocate memory.
Write a Comment
User Comments (0)
About PowerShow.com