Chapter 7 Pointers unimap

download Chapter 7 Pointers unimap

of 26

Transcript of Chapter 7 Pointers unimap

  • 8/18/2019 Chapter 7 Pointers unimap

    1/26

    Pointer

    CHAPTER 7

  • 8/18/2019 Chapter 7 Pointers unimap

    2/26

    OutlineIntroductionWhat is memory address and how to access?Example

    What are pointers?

    How to use pointers?Example !

    What are the di"erences?

    Example #Pointer ArithmeticExample $Example %

  • 8/18/2019 Chapter 7 Pointers unimap

    3/26

    OutlineArray o& pointerExample 'Example 7

    Pointer to pointerExample (

    Callin) &unction *y re&erence

    Pass ar)uments *y re&erence

    Example +Call,*y,-alue -s Call,*y,re&erence

    ???

  • 8/18/2019 Chapter 7 Pointers unimap

    4/26

    Introduction.ome C pro)rammin) tas/s are per&ormed

    more easily with pointers0

    Other tas/s cannot *e per&ormed withoutusin) pointers0

    .uch as dynamic memory allocation0

  • 8/18/2019 Chapter 7 Pointers unimap

    5/26

    What is memory address and howto access?

    E-ery -aria*le is a memory location0E-ery memory location has its address

    de1ned0

    Can *e accessed usin) ampersand 234operator0

    5enotes as an address in memory0

  • 8/18/2019 Chapter 7 Pointers unimap

    6/26

    Example

  • 8/18/2019 Chapter 7 Pointers unimap

    7/26

    What are pointers?A -aria*le whose -alue is the address o&

    another -aria*le0

    6or example direct address o& the memorylocation0

    8eneral &orm o& a pointer -aria*ledeclaration is

    type *variable_name

    type 9pointer:s *ase

    type

    -aria*le;name 9 nameo& the pointer -aria*le

  • 8/18/2019 Chapter 7 Pointers unimap

    8/26

    What are the pointers? The asteris/ < used to declare a pointer0.ame asteris/ that use &or multiplication0

    Howe-er= in this statement the asteris/ is*ein) used to desi)nate a -aria*le as apointer0

    6ollowin) are the -alid pointer declaration

  • 8/18/2019 Chapter 7 Pointers unimap

    9/26

    How to use pointers?.tep 5e1ne a pointer -aria*le0.tep ! Assi)n the address o& a -aria*le to

    a pointer0

    .tep # Access the -alue at the addressa-aila*le in the pointer -aria*le0

  • 8/18/2019 Chapter 7 Pointers unimap

    10/26

    Example !

  • 8/18/2019 Chapter 7 Pointers unimap

    11/26

    What are the di"erence?>-aria*le;num*er re&er to the -alue o& the

    -aria*le0

    >3-aria*le;num*er re&er to the addressthat contains -alue o& the -aria*le0

    >-aria*le;pointer re&er to the address o&pointer0

  • 8/18/2019 Chapter 7 Pointers unimap

    12/26

    Example #

  • 8/18/2019 Chapter 7 Pointers unimap

    13/26

    Pointer ArithmeticArithmetic operation can *e per&ormed to

    the pointer0

    6or example pointer++;

    A&ter a*o-e operation= thepointer

    willpoint to the location @@$0

    ecause each time pointer incremented= itwill point to the next inte)er location0

    Bext inte)er location is $ *ytes next to thecurrent location0

    5ecrementin) a pointer also can *e done0

  • 8/18/2019 Chapter 7 Pointers unimap

    14/26

    Example $ Incrementin) a pointer

  • 8/18/2019 Chapter 7 Pointers unimap

    15/26

    Pointer ArithmeticPointer can *e compared *y usin)

    relational operators 99= D= and 0

    I& pointer1 and pointer! point to -aria*lesthat are related to each other= thenpointer1 and pointer2 can *emeanin)&ully compared0

  • 8/18/2019 Chapter 7 Pointers unimap

    16/26

    Example % Pointercomparison

  • 8/18/2019 Chapter 7 Pointers unimap

    17/26

    Array o& Pointer There may *e a situation when we want to

    maintain an array which can store pointersto an inte)er character or any other datatype a-aila*le0

  • 8/18/2019 Chapter 7 Pointers unimap

    18/26

    Example ' Recap o& array

  • 8/18/2019 Chapter 7 Pointers unimap

    19/26

    Example 7 Array o& inte)erpointer

  • 8/18/2019 Chapter 7 Pointers unimap

    20/26

    Pointer to pointerA pointer to a pointer is a &orm o& multiple

    indirection or a chain o& pointers0

    Bormally= pointer contains the address o&-aria*le0

    Pointer to pointer contains the address o&the second pointer0

    5eclaration int

  • 8/18/2019 Chapter 7 Pointers unimap

    21/26

    Example (

  • 8/18/2019 Chapter 7 Pointers unimap

    22/26

    Callin) &unctions *yre&erenceCall *y re&erence with pointer ar)umentsPasses address o& ar)ument usin) F&:

    operator0Allows you to chan)e actual location in

    memory0Arrays are not passed with F3: *ecause the

    array name is already a pointer0

    F

  • 8/18/2019 Chapter 7 Pointers unimap

    23/26

    Passin) ar)uments to &unction *yre&erence

     Two ways to pass ar)uments to a &unctionCall,*y,-alueCall,*y,re&erence

    Call,*y,-alue is normal pass ar)uments0

    Howe-er= you can pass usin) pointer 2Call,*y,re&erence40

     This is accomplished *y applyin) the

    address operator 2&4 to the -aria*le0

  • 8/18/2019 Chapter 7 Pointers unimap

    24/26

    Example +

  • 8/18/2019 Chapter 7 Pointers unimap

    25/26

    Pass,*y,-alue -s pass,*y,re&erence

    Passin) ar)ument *y -alue Passin) ar)ument *y re&erence

    ,usin) ordinary -aria*le ,usin) pointer -aria*le

    ,usin) re)ular -aria*le to pass -alue , usin) pointer to pass -alue

    ,uses more memory space ,sa-e some memory space

    ,copy o& actual -aria*leparameter ispassed to the &unction

    ,memory address o& the-aria*leparameter is passed to the&unction

    ,any modi1cation to -aria*leparameterinside &unction will not a"ect actual-alue

    ,any modi1cation to -aria*leparameterinside &unction will a"ect actual -alue

  • 8/18/2019 Chapter 7 Pointers unimap

    26/26

    POP,GIJ

    int -alue= -alue!=