Thursday, September 22, 2011

C POINTER TO FUNCTION









REVISED: Sunday, March 15, 2015




CONTENTS:
I.   C POINTER TO FUNCTION INTRODUCTION
II.  C POINTER TO FUNCTION
III. C POINTER TO FUNCTION EXAMPLE PROGRAM SOURCE CODE
IV. C POINTER TO FUNCTION REFERENCES

YOU WILL LEARN:
1.  The C pointer to function menu application.
2.  How to take the address of a function and use a pointer to that address to call the function.

I.  C POINTER TO FUNCTION INTRODUCTION


Welcome to the “C Pointer to Function Tutorial.”


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. C POINTER TO FUNCTION


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. C POINTER TO FUNCTION EXAMPLE PROGRAM SOURCE CODE

/****************************
   C 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 TO FUNCTION\n" \
    "  \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.  C POINTER TO FUNCTION 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).

YOU HAVE LEARNED:
1.  The C pointer to function menu application.
2.  How to take the address of a function and use a pointer to that address to call the function.

Elcric Otto Circle





-->

-->

-->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"




No comments:

Post a Comment