+ Reply to Thread
Results 1 to 2 of 2

Thread: Function pointers- simple example

  1. #1
    Senior Member
    Join Date
    Oct 2006
    Location
    In Bangalore
    Age
    29
    Posts
    125
    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.

  2. #2
    Junior Member sanjana.btech's Avatar
    Join Date
    Mar 2007
    Posts
    9
    Rep Power
    8

    Re: Function pointers- simple example

    Thank you jithendra....

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C questions
    By Spoorthi in forum DATA STRUCTURES, C, C++, VC ++
    Replies: 10
    Last Post: 03-01-11, 09:15 PM
  2. Mega Free Download S/W Links A-Z
    By yathish in forum OTHERS
    Replies: 517
    Last Post: 19-07-10, 09:49 AM
  3. TCS Pacement paper 29
    By sridhar in forum TCS Placement Papers
    Replies: 0
    Last Post: 12-02-07, 05:43 PM
  4. simple vs real frd
    By Spoorthi in forum JOKES , PRANKS & QUOTES
    Replies: 10
    Last Post: 15-12-06, 09:44 PM
  5. What’s the difference between thesr
    By shilpa in forum VB / ASP.NET Technologies
    Replies: 5
    Last Post: 08-03-06, 12:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Content Relevant URLs by vBSEO 3.5.1 PL1