answersLogoWhite

0


Best Answer

Function Pointers are basically used when we want to select a function which is to be used dynamically at run time.

AnswerFunction pointers are the only way for "Interept programming". In UNIX all the Interepts are called using function pointers. This is mainly used in system programming. Answerits nothing but a pointer to function. which is similar to ptr to a variable, if we are saying ptr to a variable then it will hold address of the variable like that fn. ptr will have the address of the function..

one of the major application of the function pointer is call back function.. i.e callback.

AnswerPointers to functions/methods/subroutines aka 'Delegates' are frequently used in .NET programming especially in EventHandling, MemberInvoking

<><><>

A function pointer is used to pass a function as an argument to another function, or to store a function as a data item, for example a list of functions can be implemented as an array of pointers to functions. Function pointers are used to store interrupt handlers in tables.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

One of the big uses for function pointers in C is to call a function defined at run-time. For example, the C run-time library has two routines, qsort and bsearch, which take a pointer to a function that it calls to compare two items being sorted; this allows you to sort or search, respectively, anything, based on any criteria you wish to use.

I think the more common use of a function pointer is to generalise function calls. e.g., if there is one function called sum_gen(a, b) which in turn may require to call iself() function or isquare() which are of similar types then what we will do, we will add one function pointer argument to the sum_gen() function like:-

sum_gen(a, b, int (*fun)(void)), where fun can take either the address of iself() function or isquare function.

Now we can call sum_gen() function like:-

sum_gen(a, b, iself) or sum_gen(a, b, isquare). So its basically used to generalise the function calls.

They can be used to create 'jump table'.

enum { ADD,SUB,MUL,DIV };

double (*fptrs[])(double,double)={ add, sub, mul, divi };

(*fptrs[ operator ] ) (op1,op2);

Jump table might be more efficient and elegant than switch statement.

The other use is, when we want to call some functions sequentially one by one, in that case we declare one array of function pointers initialised to the respective functions like:-

unsigned int i ;

int (*fun[]) (void) = { fun1, fun2, fun3, fun4, fun5, fun6 };

for( i = 0; i < (sizeof (fun) / sizeof (fun[0])); i++)

(*fun [ i ])();

in this way all the function will be called sequentially instead of calling each function one by one.

Note that calling using (*f)() and f() where f is pointer to function is the same in ANSI-C. You might have f()() if f() is a function that returns pointer to function.

This answer is:
User Avatar

User Avatar

Wiki User

16y ago

they point to the address of the functions,that is, they tell us the location as to where the fuction is located or stored in a computer.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are function pointers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is virtual function table?

A virtual function table is a table of pointers to functions.


What is meant by passing function to another function in c plus plus?

Just as pointers can point to variables, pointers can also point to functions. Thus you can pass function pointers to functions. In so doing, you can alter the behaviour of the function by having it call dynamically call arbitrary functions rather than just preset functions.


Function prototype in c?

Yes. Examples can be found in stdio.h


Where are array of functions used?

Function Pointers are basically used when we want to select a function which is to be used dynamically at run time.


Why you use pointers?

Because you can produce fast and efficient code. Function arguments are passed "by value", and so you can't change the original value of the argument, but if you use pointers, you can.


What are functions and pointers in c?

function-these are self contained block of statements to solve a particular task whereas pointers are defined as the variable which stores the address of another variable


Is Use of pointers in function saves the memory space?

No. But avoiding unnecessary duplication of data does.


Is it possibly to return an array of strings in a function without using pointers in C plus plus?

No.


How can you use pointers as function arguments?

Like any other value/variable -- nothing special.


When do you use pointers in C?

There are many uses of pointer in C. Pointers are efficient and elegant to use. All string variables are pointers, and calling a function using a pointer allows the function to change the value globally. These are what you can do with pointers in C. 1) Returning multiple values by passing address of variables. eg. foo(&amp;a,&amp;b,&amp;c); 2) When you want to modify the value passed to function. eg. scanf() function. int n; scanf("%d",&amp;n); /* pass address of variable n so that scanf can change its value*/ 3) When you need to pass large data structure to function, it is more efficient to pass pointer to structure than passing the entire structure. If you don't want to modify the structure members, you can use 'const' keyword which the ANSI-complaint C compiler will either warn or give error if you modify. eg strlen(const char *str) in string library should not modify the content of str. 4) Implementing 'goto' data structures are easy with pointers. eg. linked-list,binary trees, hash table. 5) You can allocate dynamic memory using pointers. 6) Pointers to function are used for call back function and jump table. eg qsort() and bsearch() functions require the caller to provide pointer to function to do the comparison.


How do declare function pointer having two integer parameters and returning integer pointers?

// declare a function int* function(int, int); or int* (function)(int, int); // declare a pointer to a function int* (*pointer_to_function)(int, int);


Why pointers are required?

The most important use of pointers comes when we pass value by reference to any function. You do not need to create a second memory location as in pass by value. You can mofify the original variable by using its address.