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.