![]() |
|
| ds. c , c++ Interview / Technical Questions Kindly solve an many as questions you can. It will sharpen your skills and those solutions will help others too. |
![]() |
| LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) |
| Senior Member Join Date: Oct 2006 Location: In Bangalore Age: 25
Posts: 126
Thanks: 1
Thanked 9 Times in 9 Posts
Rep Power: 0 | Function pointers- simple example Let's discuss briefly about Function pointers... In C we know that each and every variable has got address. Similarly each and every function has an address associated with it. In order to know the address of the function we use the following statement.. printf("Address of the function = %u",func); //func is some function. So it is obvious that we can obtain the address of a function by specifying its name. Now observe the statement below.. ptr = func; Here func is the address of the function func. This address is being assigned to ptr. Now the point is how do we declare ptr.. we declare it as... (*ptr)(); Since ptr contains address it must be a pointer. In this case the pointer is containing the address of the function so the declaration goes as (*ptr)() which says ptr is a pointer to a function which does not have any arguments... We will put all this discussion to work by considering an example... #include<stdio.h> int main() { int fun(int,int); int (*ptr)(int,int); printf("The address of the function is %u\n",fun); ptr = fun; (*ptr)(2,3); } int fun(int a,int b) { int res; res = a+b; printf("%d",res); } output: 5 Explanation : Here int (*ptr)(int,int) indicates that ptr is a pointer to a function which takes two arguments and returns an integer. We can call the function fun by using the address of it. Thus a call is made to it by the statement (*ptr)(2,3)... The rest is simple. Advantage of Function pointers : The primary advantage with function pointer is that a function can be called at run time instead of compile time. |
| | |
![]() |
| Tags |
| function , pointers , simple |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C questions | Spoorthi | DATA STRUCTURES, C, C++, VC ++ | 9 | 05-11-09 02:18 PM |
| Mega Free Download S/W Links A-Z | yathish | OTHERS | 516 | 21-03-09 10:06 AM |
| TCS Pacement paper 29 | sridhar | TCS Placement Papers | 0 | 12-02-07 05:43 PM |
| simple vs real frd | Spoorthi | JOKES , PRANKS & QUOTES | 10 | 15-12-06 09:44 PM |
| Whats the difference between thesr | shilpa | VB / ASP.NET Technologies | 5 | 08-03-06 12:09 PM |
| More Interview Questions Here... |