Pointer to Function

M

Mike Wahler

chook said:
How i can call my function with some parameters,
if i have a pointer to this function

#include <stdio.h>

int func(int arg)
{
return arg * 2;
}

int main(void)
{
int (*fp)(int) = func;
int ans = fp(42);

printf("%d\n", ans); /* prints 84 */
return 0;
}


Which C book(s) are you reading?

-Mike
 
K

Keith Thompson

chook said:
How i can call my function with some parameters,
if i have a pointer to this function

The same way you call any function with parameters.

Every function call uses a function pointer. In an ordinary call
like:

void my_func(int foo) { }
my_func(42);

the name "my_func" in the call is implicitly converted to a pointer to
the function.
 
I

infobahn

chook said:
How i can call my function with some parameters,
if i have a pointer to this function

See Kernighan and Ritchie's "The C Programming Language", 2nd edition,
pp 118ff
 
D

dandelion

chook said:
How i can call my function with some parameters,
if i have a pointer to this function

int (*func_ptr)(int a, int b);

func_ptr = foobar;

int result = (*func_ptr)(arg1, arg2);

HTH dandelion.
 
C

chook

How i can call my function with some parameters,
if i have a pointer to this function
 
K

Keith Thompson

dandelion said:
int (*func_ptr)(int a, int b);

func_ptr = foobar;

int result = (*func_ptr)(arg1, arg2);

The "*" operator is optional. You can also do:

int result = func_ptr(arg1, arg2);

(Arguably the "*" makes it clearer that you're using a explicit
function pointer.)
 
D

dandelion

Keith Thompson said:
The "*" operator is optional. You can also do:

int result = func_ptr(arg1, arg2);

(Arguably the "*" makes it clearer that you're using a explicit
function pointer.)

But the second avoids the clutter and is argueably easyer to read..
 
J

Jonathan Burd

chook said:
How i can call my function with some parameters,
if i have a pointer to this function

The following example should show you how to use function pointers:

/*
* Note 1: strcmpi is non-standard.
* Note 2: Use the cleaner alternatives.
*/

#include <stdio.h>
#include <string.h>
#include <stddef.h>

typedef int (*foo) (const char *, const char *);

int func_strcmp (
const char *,
const char *,
int (*) (const char*, const char*)
);

int main (int argc, char ** argv)
{
int (*foocmp) (const char *, const char *);
size_t (*foolen) (const char *);
int retcmp; /* stores return value from comparisons */
size_t slen; /* stores return value from foolen */

/* Using typedef for an array of function pointers */
foo cmp[2] = {strcmp, strcmpi};

/* Array of function pointers without using typedef */
int (*barcmp[2]) (const char *, const char *) = {strcmp, strcmpi};


foocmp = &strcmp; /* Alternative Syntax */
foolen = strlen; /* Normal */


/* Cleaner syntax */
slen = foolen ("Direct");
/* Alternative syntax */
retcmp = (*foocmp) ("Indirect", "iNdIrEcT");
printf ("Length: %u\nComparison 1: %d\n", slen, retcmp);


/* typedef'd array calls */
retcmp = cmp[0] ("Test", "test");
printf ("Comparison 2: %d\n", retcmp);
retcmp = (*cmp[1]) ("Test", "test");
printf ("Comparison 3: %d\n", retcmp);


/* Alternative Array syntax calls. */
retcmp = barcmp[0] ("Test", "test");
printf ("Comparison 4: %d\n", retcmp);
retcmp = (*barcmp[1]) ("Test", "test");
printf ("Comparison 5: %d\n", retcmp);


/* passing function pointers as arguments */
retcmp = func_strcmp ("Test", "test", strcmp);
printf ("Comparison 6: %d\n", retcmp);

return 0;
}

/* demonstrates how to pass function pointers as arguments */
int func_strcmp (
const char * str1,
const char * str2,
int (*cmp) (const char*, const char*)
)
{
return cmp (str1, str2);
}

/* EOC */

Please read the comments in the code to understand the code. I hope this
helps you. If you think, there's something wrong with the code, please
feel free to comment.

Regards,
Jonathan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,415
Latest member
SharonCran

Latest Threads

Top