Pointers - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Pointers

Description:

What values of x and y is printf going to print? why? Now consider this: int main ... What values of x and y are printed here? why? Passing by Reference ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 10
Provided by: alanb9
Category:
Tags: pointers | prints

less

Transcript and Presenter's Notes

Title: Pointers


1
Pointers
  • Chapter 14

2
What is a pointer?
  • A pointer is a variable that holds a memory
    address.
  • Pointers are an indirect way to access an object
    and thus the use of pointers is sometimes called
    indirection.

3
Defining a Pointer
  • A pointer variable is defined by placing a in
    front of the variable name.
  • long temp NULL
  • char c NULL
  • short a,b
  • the is considered part of the variable name
    during declaration. The above definition is not
    the same as short a,b In this declaration, a
    would be a pointer to a short, and b would be a
    short.
  • Even though youre technically declaring youll
    be storing a memory address, you still need to
    specify what type of data the pointer points to.
  • You should always set your variables equal to
    NULL at the time of declaration. By default,
    theyre given a random address and use of the
    pointer could cause data corruption and will most
    likely cause a program and/or OS crash.

4
Using Pointers
  • To set a pointer to point to an already existing
    variable, you need to use the address operator,
  • short a77short tempNULLtemp a
  • temp now holds the address to variable a.

5
Using Pointers
  • To modify or retrieve the data that a pointer
    points to, you need to use the dereference
    operator,
  • short a77short tempNULLtemp a //set
    temp to point to atemp 2 //set the stuff
    being pointed at by temp to 2printf(ad
    \n,temp) //prints the value of temp
    printf(ad \n,a) //prints the value of a
  • Essentially, in the above example, using temp is
    equivalent to using a directly. Using variables
    directly is much faster than using them
    indirectly through pointers, so you should use
    them directly whenever possible.

6
What if .
  • What if in the last example, I had forgotten to
    use the dereference operator when using temp?
  • temp 2
  • temp2 would have caused temp to point to memory
    address 2. Any use of temp after this would
    cause either a crash or data corruption.
  • printf(ad \n,temp)
  • printf(ad \n,temp) would have printed the
    memory address of temp rather than the data being
    pointed at by temp.

7
Pointers Functions
  • Consider this code
  • int main( )short x 1, y2swap(x,y)printf(x
    d, yd,x,y)return 0int swap( short a,
    short b)short temp aa bb tempreturn
    0
  • What values of x and y is printf going to print?
    why?

8
  • Now consider this
  • int main( )short x 1, y2swap(x,y)printf(
    xd, yd,x,y)return 0int swap( short
    a, short b)short temp aa bb
    tempreturn 0
  • What values of x and y are printed here? why?

9
Passing by Reference
  • Using pointers to pass data to a function is
    called passing by reference. It allows you to
    manipulate the data of the calling function from
    within the called function which normally would
    be unavailable. It effectively allows you to
    return multiple values back to the calling
    function.
Write a Comment
User Comments (0)
About PowerShow.com