473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Pointer to Function

How i can call my function with some parameters,
if i have a pointer to this function
Nov 14 '05 #1
8 2208

"chook" <ch***@top7.ntu-kpi.kiev.ua> wrote in message
news:cr***********@news.ntu-kpi.kiev.ua...
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
Nov 14 '05 #2
chook <ch***@top7.ntu-kpi.kiev.ua> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
chook wrote:

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
Nov 14 '05 #4

"chook" <ch***@top7.ntu-kpi.kiev.ua> wrote in message
news:cr***********@news.ntu-kpi.kiev.ua...
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.
Nov 14 '05 #5
"dandelion" <da*******@meadow.net> writes:
"chook" <ch***@top7.ntu-kpi.kiev.ua> wrote in message
news:cr***********@news.ntu-kpi.kiev.ua...
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);


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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"dandelion" <da*******@meadow.net> writes:
"chook" <ch***@top7.ntu-kpi.kiev.ua> wrote in message
news:cr***********@news.ntu-kpi.kiev.ua...
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);


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..
Nov 14 '05 #7
chook wrote:
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.
Nov 14 '05 #8
chook wrote:
How i can call my function with some parameters,
if i have a pointer to this function


Try:
http://www.function-pointer.org/

--
K
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
16
by: fix | last post by:
Hi all, I am new to C and I just started to program for a homework. I included my code down there. It is a fraction "class". I declared the Fraction struct, tried to create some invalid fraction,...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.