Dale Roberts, Lecturer - PowerPoint PPT Presentation

About This Presentation
Title:

Dale Roberts, Lecturer

Description:

int descending( int a, int b ) { return b a; /*swap if b is greater than a ... this determines ascending or descending sorting ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 12
Provided by: Jeffre
Category:

less

Transcript and Presenter's Notes

Title: Dale Roberts, Lecturer


1
Department of Computer and Information
Science,School of Science, IUPUI
CSCI 230
Pointers Pointer Arithmetic
  • Dale Roberts, Lecturer
  • Computer Science, IUPUI
  • E-mail droberts_at_cs.iupui.edu

2
Pointer Expressions and Pointer Arithmetic
  • pvn
  • ? pv nsizeof(variable type that pointer point
    to)
  • Arithmetic operations can be performed on
    pointers
  • Increment/decrement pointer ( or --)
  • Example
  • vPtr, vPtr,
  • --vPtr, vPtr--
  • Add an integer to a pointer( or , - or -)
  • Pointers may be subtracted from each other
  • Operations meaningless unless performed on an
    array

 
3
  • Example
  • Five element int array on machine with 4 byte
    ints
  • vPtr points to first element v 0 whose address
    location is 3000 (vPtr 3000)
  • vPtr 2
  • // sets vPtr to 3008
  • vPtr points to v 2 (incremented by 2), but
    the machine has 4 byte integers, so it points to
    address 3008

4
Pointer Expressions and Pointer Arithmetic
  • Example (double pointer)
  • Assume long (long integer) is 4 bytes, and
    pointer variable is 2 bytes.
  • long a105, 10, 15,
  • long pa, ppa
  • int i5
  • pa a
  • ppa pa

Questions
Expression Value Note
pa1 644 64014
pa3 652 64034
pai 660 640i4
ppa1 702 70012
ppai 710 700i2
pa1 6 51
(pa1) 10 a1pa1(a1)
pa2 15 648
ppa 640 value of pa
ppa1 644 pa1
(ppa1) invalid (702)
ppa1 6 a01 51
(ppa1) 10 (pa1)(64014)
Variable Address Value
a 640 5
644 10
648 15

pa 700 640
ppa 800 700
5
Pointer Expressions and Pointer Arithmetic
  • Subtracting pointers
  • Returns number of elements from one to the other.
    If
  • vPtr2 is a pointer pointing to v 2
  • vPtr is a pointer pointing to v 0
  • vPtr2 - vPtr would produce 2
  • Pointer comparison ( lt, , gt )
  • See which pointer points to the higher numbered
    array element
  • Also, see if a pointer points to 0
  • Pointers of the same type can be assigned to each
    other
  • If not the same type, a cast operator must be
    used
  • Exception pointer to void (type void )
  • Generic pointer, represents any type
  • No casting needed to convert a pointer to void
    pointer
  • void pointers cannot be dereferenced

6
The Relationship Between Pointers and Arrays
  • Arrays and pointers are closely related
  • Array name like a constant pointer
  • Pointers can do array subscripting operations
  • Example Declare an array b 5 and a pointer
    bPtr
  • bPtr b
  • // To set them equal to one another
  • // The array name (b) is actually the address of
    first element of the array
  • bPtr b 0
  • // Explicitly assigns bPtr to address of first
    element of b
  • To access element b 3
  • x( bPtr 3 ) // Where n is the offset. Called
    pointer/offset notation
  • xbptr 3 // Called pointer/subscript notation
  • // bPtr 3 same as b 3
  • x( b 3 ) // Performing pointer arithmetic on
    the array itself

7
Pointers and Arrays
  • Strong relation between pointers and arrays
  • Pointers and arrays can be used interchangeably.
  • The array name is equivalent to the address of
    the first element in the array
  • Example
  • int a10
  • int pa
  • pa a0 / is equivalent to pa a /
  • So, a1 ? (pa1) ? pa1 ? (a1)
  • a1 ? pa1 ? a1
  • ai ? (pai) ? pai ? (ai)
  • ai ? pai ? ai
  • ai5 ? (pai)5 ? pai5
  • Example
  • f(int s)

8
Arrays of Pointers
  • Arrays can contain pointers
  • For example an array of strings
  • char suit4 "Hearts", "Diamonds",
  • "Clubs", "Spades"
  • Strings are pointers to the first character
  • char each element of suit is a pointer to a
    char
  • The strings are not actually stored in the array
    suit, only pointers to the strings are stored
  • suit array has a fixed size, but strings can be
    of any size

 
9
Pointers to Functions
  • Pointer to function
  • Contains address of function
  • Similar to how array name is address of first
    element
  • Function name is starting address of code that
    defines function
  • Function pointers can be
  • Passed to functions
  • Stored in arrays
  • Assigned to other function pointers

10
Example Bubble Sort
  • define SIZE 10
  • void bubble(int ,const int,
  • int ()(int,int))
  • int ascending( int, int )
  • int descending( int, int )
  • int main()
  • int aSIZE 2, 6, 4, 8, 10,
  • 12, 89, 68, 45, 37
  • bubble( a, SIZE, ascending )
  • bubble( a, SIZE, descending )
  • void bubble( int work, const int size,
  • int (compare)(int,int))

void swap( int element1Ptr, int
element2Ptr ) int temp temp
element1Ptr element1Ptr element2Ptr elemen
t2Ptr temp int ascending( int a, int b )
return b lt a /swap if b is less than
a/ int descending( int a, int b ) return b
gt a /swap if b is greater than a/
11
  • Function bubble takes a function pointer
  • bubble calls this helper function
  • this determines ascending or descending sorting
  • The argument in bubblesort for the function
    pointer
  • bool ( compare )( int, int )
  • tells bubblesort to expect a pointer to a
    function that takes two ints and returns a bool
  • If the parentheses were left out
  • bool compare( int, int )
  • Declares a function that receives two integers
    and returns a pointer to a bool
Write a Comment
User Comments (0)
About PowerShow.com