+ Reply to Thread
Results 1 to 1 of 1

Thread: function pointer

  1. #1
    Chinmoy's Avatar
    Chinmoy is offline Programming Expert
    Join Date
    Feb 2008
    Location
    where heaven meets earth
    Posts
    410
    Rep Power
    18

    function pointer

    Function Pointers are pointers, i.e. variables, which point to the address of a function. Thus they exhibit polymorphism by call by different methods. Function pointers is an exclusive propery of the c language and should be used wisely as functions are always more powerful and fact in execution as compared to function pointers.

    Consider the following example.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void new_(int);
    void old(int);
    
    void main(void)
    {
    	  void (*funcpoint)(int);
    	  int number;
    	  clrscr();
    	  printf("Enter a number");
    	  scanf("%d", &number);
    	  funcpoint = (number > 5) ? old : new_;
    	  funcpoint(number);
    	  getch();
    }
    
    void new_(int n)
    {
    	  printf("The number %d, is less or equal to 5.\n", n);
    }
    void old(int m)
    {
    	  printf("The number %d, is more than 5.\n", m);
    }
    The function pointer *funcpoint is declared here as void (*funcpoint)(int), which specifies both the return type (void) and the types of arguments (int) of the function. We then assign the pointer to a particular function. Now we can call the function normally. This gives a double nature to teh function.
    Attached Thumbnails Attached Thumbnails function pointer-1.jpg  
    Attached Files Attached Files
    God is real... unless declared an integer

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
+ 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. passing pointer to function
    By alirezan in forum C and C++
    Replies: 2
    Last Post: 09-20-2011, 07:34 AM
  2. Function pointer problem
    By DarkLordofthePenguins in forum C and C++
    Replies: 4
    Last Post: 08-28-2011, 03:00 PM
  3. Replies: 1
    Last Post: 03-20-2011, 06:16 AM
  4. function pointer .. help
    By sp3tsnaz in forum C and C++
    Replies: 4
    Last Post: 01-07-2010, 11:22 AM
  5. Pointer to pointer and pointer in function
    By Max.89 in forum C and C++
    Replies: 3
    Last Post: 04-05-2009, 06:34 AM

Tags for this Thread

Bookmarks

Posting Permissions

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