CS 2130 - PowerPoint PPT Presentation

About This Presentation
Title:

CS 2130

Description:

CS 2130 – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 29
Provided by: ble87
Category:
Tags: fact

less

Transcript and Presenter's Notes

Title: CS 2130


1
CS 2130
  • Lecture 12a
  • Void Pointers
  • Function Pointers

2
Void Pointers
  • We talked about void pointers in an earlier
    lecture
  • We stressed some things not to do
  • We also mentioned that they had useful purposes
  • For example
  • malloc returns a void
  • It is just returning a pointer to some newly
    allocated space
  • The space has no associated type
  • The programmer must make that association by
    assigning the returned value into the appropriate
    type pointer
  • Since this type isn't known to the write of
    malloc it returns the generic void

3
Another Example
  • One of the problems with the standard swap( )
    function is that it is "hard-wired" to a
    particular type (i.e. we must know ahead of time
    that we are swapping two ints, or two floats,
    etc).
  • Write a swap( ) function that will swap two items
    of arbitrary size. Your function will need to
    accept a third parameter, which is the sizeof the
    two items to be swapped. Do not use any library
    calls in your solution.

4
  • void swap(void a, void b, size_t size)
  • char ap a
  • char bp b
  • int i
  • for(i0 iltsize i)
  • char t api
  • api bpi
  • bpi t

5
Questions?
6
Function Pointers
7
What is...
  • A variable?
  • A symbol representing an address where we store
    something
  • An array variable?
  • A symbol representing an address where a block of
    memory has been reserved
  • A function name?
  • A symbol representing the address of a piece of
    code which we can jump to (and we normally expect
    it will contain code that will return control to
    the caller.)
  • Assembly language for int fact(int n)
  • fact save sp, -112, sp Beginning of
  • fact function

8
If we...
  • Want to store the address of a variable we use a
    pointer (e.g. int ip)
  • Same is true for holding the address of a
    function
  • int fi(void) / Function that returns an int /
  • int pfi(void) / Function that returns a
    pointer to
  • an int, should be fpi /
  • (int )fpi(void) / What compiler sees /
  • int (pfi)(void) / Declaring pfi to be a
    pointer to
  • a function! /

9
Using it...
  • int fi(void) / Function that returns an int
    /
  • int fpi(void) / Function that returns a
    pointer /
  • / to an int
    /
  • int (pfi)(void) / Declaring pfi to be a
    pointer /
  • to a function!
    /
  • pfi fi
  • pfi fi() / NO NO NO NO NO NO NO NO NO
    NO /
  • Notice similarity to
  • int ia10
  • int ip
  • ip ia

10
Using it (Part Deux)...
  • i fi()
  • i pfi() ALL THE SAME!!!
  • i pfi()
  • Notice similarity to
  • int ia10
  • int ip
  • ip ia

11
But what good is a function pointer?
  • Say you are writing a general purpose sorting
    function.
  • You want it to be able to sort anything
  • Numbers
  • Strings
  • ???
  • Obviously comparing numbers and strings calls for
    two different techniques
  • What if we write functions that do the comparison
    we need
  • A function to compare numbers
  • A function to compare strings
  • A function to compare ???

12
But what good is a function pointer?
  • Now when we call the function to do the sorting
    we pass in a pointer to the appropriate function
    for the type of data we have!
  • Or
  • Imagine we are writing a routine to traverse a
    data structure
  • What exactly do we do at each node?
  • print it?
  • modify it?
  • test it?

13
But what good is a function pointer?
  • What if we passed into our traversal routine a
    pointer to a function which did what needed to be
    done???
  • Advantage No need to continually modify our
    traversal routine. It's now generic!

14
Basic Treenode with Root Pointer
  • struct treenode
  • char itemName
  • int itemCount
  • struct treenode left, right
  • struct treenode root NULL
  • int totalItems

15
Post Order Traversal
  • void
  • postOrder( struct treenode theNode,
  • void ( doThis )( struct treenode ) )
  • if( theNode NULL )
  • return
  • postOrder( theNode-gtleft, doThis )
  • postOrder( theNode-gtright, doThis )
  • doThis( theNode )

16
Closer Look
  • void
  • postOrder( struct treenode theNode,
  • void ( doThis )( struct treenode ) )
  • Note This function "postOrder" take two
    parameters
  • A pointer to a treenode
  • A pointer to a function

17
Closer Look
  • void
  • postOrder( struct treenode theNode,
  • void ( doThis )( struct treenode ) )

Function Pointer
Parameter list for function
Return value of function
18
Counting Function
  • void
  • countItems( struct treenode theNode )
  • totalItems theNode-gtitemCount

19
Printing Function
  • void
  • printItems( struct treenode theNode )
  • PRINTF(( "s d\n", theNode-gtitemName,
    theNode-gtitemCount ))

20
Less than 5 items Function
  • void
  • checkItems( struct treenode theNode )
  • if( theNode-gtitemCount lt 5 )
  • PRINTF(( "s\n", theNode-gtitemName ))

21
Clear Items Function
  • void
  • clearItems( struct treenode theNode )
  • theNode-gtitemCount 0

22
Free Nodes Function
  • void
  • freeNodes( struct treenode theNode )
  • free( theNode-gtleft )
  • free( theNode-gtright )
  • free( theNode-gtitemName )

23
Main Function
  • int
  • main( void )
  • root buildTree( )
  • totalItems 0 / Global /
  • postOrder( root, countItems )
  • PRINTF(( "There are d items.\n",
  • totalItems ))

24
Main Function
  • PRINTF(( "List of all the items and"
  • " how many there are\n" ))
  • postOrder( root, printItems )
  • PRINTF(( "The following items are in"
  • " short supply\n" ))
  • postOrder( root, checkItems )
  • / clear itemCount for all items /
  • postOrder( root, clearItems )

25
Main Function
  • / free all nodes /
  • postOrder( root, freeNodes )

26
NOTE
  • Function Header
  • void
  • postOrder( struct treenode theNode,
  • void ( doThis )( struct treenode ) )
  • Function Call
  • doThis( theNode )
  • Function Header
  • void
  • countItems( struct treenode )
  • Function Call
  • postOrder( root, countItems )

27
Questions?
28
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com