Page 1 of 1

C POINTER TUTORIAL PART 5 POINTERS TO FUNCTIONS Rate Topic: ***** 1 Votes

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 03 July 2010 - 07:36 AM

C POINTER TUTORIAL
PART 5


CONTENTS
• I. INTRODUCTION
• II. POINTERS TO FUNCTIONS
• III. EXAMPLE PROGRAM
• IV. REFERENCES


I. INTRODUCTION

Hello; nice to meet you. Welcome to the “C Pointer Tutorial Part 5.”

This tutorial describes taking the address of a function and using a pointer to that address to call the function.

For example, the next time you code a “user friendly” program that prompts the user to enter a value inorder to select an option from a menu, instead of using a switch and cases, use an array of function pointers. Code the user’s choice as an integer subscript into the array of function pointers, and have each function pointer call the appropriate menu selection function. This process is described in the example below.


II. POINTERS TO FUNCTIONS

A pointer to a function contains the address of the function in memory. This is similar to an array where the name of the array is a pointer to the base address of the array.

The pointer to a function must be the same “type” as the function. The pointer to a function can be:


A. Assigned to other function pointers.

A pointer to a function is dereferenced to execute the function.

B. Passed to other functions.

The passed function must be “external,” “global” to the program, and defined outside any function boundaries.

A pointer can be used to invoke a function.


C. Returned from other functions.

D. Stored in arrays.

E. Used to call the function.

Always remember, calls to a function through a pointer and calls directly to a function both use the same rules for argument passing.

III. EXAMPLE PROGRAM

/* ***************************
   C POINTER TUTORIAL PART 5
   POINTER TO FUNCTION
   *************************** */
#include<ctype.h>     /*  Character Class Tests  */
#include<math.h>      /*  Math Header.           */
#include<stdio.h>     /*  Standard I/O.          */
#include<stdlib.h>    /*  Utility Functions.     */

#define HEADER \
    "\n\n  C POINTER TUTORIAL PART 5\n" \
	"  POINTER TO FUNCTION\n\n" \
	"  0  Execute function zero.\n" \
    "  1  Execute function one.\n" \
    "  2  Execute function two.\n" \
	"                                             \n"\
    "  Type number 0, 1 or 2 and then press Enter.\n"\
	"                                             \n"\
	"  EXIT by typing 3 and then pressing Enter.    "
/*
	FUNCTION PROTOTYPES
*/
void zero (int);
void one (int);
void two (int);

int getInteger(int *maybe)
{
        char alpha, buff [ 80 ]; 
        return fgets(buff, sizeof buff, stdin) && !isspace(*buff) &&
        sscanf(buff, "%d%c", maybe, &alpha) == 2 && (alpha == '\n' || alpha == '\0');
}

int main( int argc, char* argv[] )
{
	/*  Initialize array of three pointers to functions
	    that each take an int argument and return void.  */
	void(*ptr2Fcn[3])(int)= {zero, one, two};

	printf(HEADER);
	
	int menuSelection;
    do
	{	
		fflush(stdout);
    } while ( !getInteger(&menuSelection) );
	
	while((menuSelection >= 0) && (menuSelection <  3))
	{
		/*  Invoke the function at location menuSelection
		    in the array ptr2Fcn and pass menuSelection as an argument.  */
		(*ptr2Fcn[menuSelection]) (menuSelection);

		printf(HEADER);

		do
		{	
			fflush(stdout);
        } while ( !getInteger(&menuSelection) );
	}

	return 0;
}

/*
	FUNCTION DEFINITIONS
*/

void zero(int x)
{
	printf ("\n\n  You entered %d; therefore, function zero was called.\n\n", x);
}

void one(int y)
{
	printf ("\n\n  You entered %d; therefore, function one was called.\n\n", y);
}

void two(int z)
{
	printf ("\n\n  You entered %d; therefore, function two was called.\n\n", z);
}



IV. REFERENCES

The C Programming Language by Brian Kernighan and Dennis Ritchie (Englewood Cliffs, N.J.: Prentice-Hall, 1978).

C++: The Complete Reference, Fourth Edition by Herbert Schildt (Berkeley, California: McGraw-Hill/Osborne, 2003).


Is This A Good Question/Topic? 1
  • +

Page 1 of 1