An example of using a pointer to a function. It must be pointed out that there is nothing new here, the pointer to a function is available in ANSI-C as well as in C++ and works in the manner described here for both languages. It is not regularly used by most C programmers, so it is defined here as a refresher.

[sourcecode language=’cpp’]

/*******************************************************
* MYCPLUS Sample Code – http://www.mycplus.com *
* *
* This code is made available as a service to our *
* visitors and is provided strictly for the *
* purpose of illustration. *
* *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/

#include

void print_stuff(float data_to_ignore);
void print_message(float list_this_data);
void print_float(float data_to_print);
void (*function_pointer)(float);

main()
{
float pi = 3.14159;
float two_pi = 2.0 * pi;

print_stuff(pi);
function_pointer = print_stuff;
function_pointer(pi);
function_pointer = print_message;
function_pointer(two_pi);
function_pointer(13.0);
function_pointer = print_float;
function_pointer(pi);
print_float(pi);
}

void print_stuff(float data_to_ignore)
{
printf(“This is the print stuff function.\n”);
}

void print_message(float list_this_data)
{
printf(“The data to be listed is %f\n”, list_this_data);
}

void print_float(float data_to_print)
{
printf(“The data to be printed is %f\n”, data_to_print);
}

// Result of execution
//
// This is the print stuff function.
// This is the print stuff function.
// The data to be listed is 6.283180
// The data to be listed is 13.000000
// The data to be printed is 3.141590
// The data to be printed is 3.141590
[/sourcecode]

0 Shares
Share
+1
Tweet
Share
Stumble