Page 1 of 1

## C POINTER TUTORIAL PART 5 POINTERS TO FUNCTIONS Rate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=180050&amp;s=33735923940a9283bdd327152f311a87&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Elcric

• D.I.C Regular

Reputation: 102
• 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<stdio.h>     /*  Standard I/O.          */
#include<stdlib.h>    /*  Utility Functions.     */

"\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};

do
{
fflush(stdout);

{
/*  Invoke the function at location menuSelection
in the array ptr2Fcn and pass menuSelection as an argument.  */

do
{
fflush(stdout);
}

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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }