Re: function pointer tutorial may be helpful..



devesh said:

function pointer is a variable which points to the address
of a function.

A function pointer is a value that is the address of a function. A function
pointer is said to "point to a function" rather than "point to the address
of a function".

it is basically used for removing/replacing switch statement and if
else statement.

That is one possible use.

it is very usefull to implement callback function.callback functions
are mostely
used in driver writing.

Actually, they are used much more widely than that.

it can also be used insted of virtual function
to save some time...

C doesn't have virtual functions.

now consider this example to understand how function pointer works...


#include<stdio.h>
static int my_function(int a)
{

printf("my_function :%d",a);
return(2*a+3);
}
int main(void)
{
int (*new_function)(int)= my_function; /* here a function
new_function is decleared,

No. There is no function by that name; new_function is not a function, but
a function pointer.

the address of the function named
my_function is assigned to it, and function is invoked by
dereferencing the pointer.*/

Right.

<snip>

now consider another example..

void young(int);
void old(int);

int main(void) {
void (*fp)(int);
int age;
printf("How old are you? ");

When you use any variadic function (and that certainly includes printf),
you need to provide a valid function prototype for it. You can do this by
adding:

#include <stdio.h>

at the top of your code. If you don't provide such a prototype, the
behaviour of your program is undefined. (The same applies to scanf and
other variadic functions.)

scanf("%d", &age);

Check that this scanf call succeeded. The scanf function returns a useful
value that you should test to ensure that it's what you expect.

<snip>

Void arithematic(float a,float b,float (*operation_arithematic)
(float,float));

C has no Void type. If you don't define it yourself, it doesn't exist.
(Hint: C is case-sensitive.)

<snip>

void main( )

In C, main returns int.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • Re: returning function pointer
    ... function pointer some_func & meanwhile calls that function and prints ... void some_func ... int main ... Note there are two parameter lists here. ...
    (comp.lang.c)
  • Re: how to remove code duplication
    ... void create_logname(char *, int); ... int find_logsize(const char *); ... <snip some code> ...
    (comp.lang.c)
  • Re: Casting pointers to functions of different types
    ... you will receive an error message from the compiler. ... Code which stored function pointer in array of unsigned ... typedef void; ... void marshal (AFunc func, int *args) ...
    (comp.lang.c)
  • Casting pointers to functions of different types
    ... It has to cast a function pointer to some ... Code which stored function pointer in array of unsigned ... typedef void; ... void marshal (AFunc func, int *args) ...
    (comp.lang.c)
  • Re: get the address of a function??
    ... void hello ... Calling printf with no visible prototype invokes undefined behavior. ... Make it "int main". ... *But* there's no defined conversion from a function pointer to void* ...
    (comp.lang.c)