Thread: Function pointer issue

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    14

    Function pointer issue

    Function pointer is use to call function by it's address Why program doesn't print statement ?

    Code:
    #include <stdio.h>
    
    void colour ()
    {
        printf("Green colours");
    }
    
    
    int main ()
    {
        void (*fun_ptr)(void) = &colour; 
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,713
    It no more calls the function, than saying
    int answer = 42;
    causes something to be seen on screen.

    You need to call your function via it's pointer if you want something to happen.

    Code:
        void (*fun_ptr)(void) = &colour; 
        fun_ptr();  // call it!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    14
    Quote Originally Posted by Salem View Post
    It no more calls the function, than saying

    You need to call your function via it's pointer if you want something to happen.
    oh Thank you What's wrong with code

    Code:
     #include <stdio.h> 
    void addition (int a, int b)
    {
        int c = a + b; 
        
        printf ("c = %d /n", c);
    }
     
     
    int main ()
    {
        addition (2, 5);
        
        void (*fun_ptr)(int) = &addition; 
        fun_ptr(); 
        return 0;
    }
    main.c:24:5: error: too few arguments to function ‘fun_ptr’
    fun_ptr();
    ^~~~~~~

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    The error message is telling you exactly what's wrong: too few arguments to function ‘fun_ptr’
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    14
    Quote Originally Posted by laserlight View Post
    The error message is telling you exactly what's wrong: too few arguments to function ‘fun_ptr’
    But I am passing two actual argument in main function
    gone through following links
    Too few arguments to function (C language Error)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,713
    > But I am passing two actual argument in main function
    Yeah, but only when you're calling addition directly.

    > void (*fun_ptr)(int) = &addition;
    This is just a bunch of lies - addition takes two int parameters.

    > fun_ptr();
    More lies - you don't even supply one parameter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2020
    Posts
    14
    Quote Originally Posted by Salem View Post
    > But I am passing two actual argument in main function
    Yeah, but only when you're calling addition directly.

    > void (*fun_ptr)(int) = &addition;
    This is just a bunch of lies - addition takes two int parameters.

    > fun_ptr();
    More lies - you don't even supply one parameter.
    Code:
    #include <stdio.h> void addition (int a, int b)
    {
        int c = a + b; 
         
        printf ("c = %d \n", c);
    }
      
      
    int main ()
    {
        addition (2, 5);
         
        void (*fun_ptr)(int, int) = &addition; 
    //    fun_ptr(2, 5); 
        return 0;
    }
    c = 7

    There is no need to use this fun_ptr(2, 5);

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,713
    So delete line 11 and uncomment line 14.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Apr 2020
    Posts
    7
    Code:
    void (*fun_ptr)(void) = &colour;
    You can leave off the & when assigning the colour function to the pointer. In C, a function name is a pointer to the function's code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function pointer issue
    By laurick24i in forum C Programming
    Replies: 2
    Last Post: 02-15-2020, 03:48 AM
  2. Issue with function pointer and template
    By CodeSlapper in forum C++ Programming
    Replies: 6
    Last Post: 08-18-2016, 10:48 PM
  3. Pointer to Pointer manipulation issue
    By workisnotfun in forum C Programming
    Replies: 5
    Last Post: 10-16-2012, 11:31 AM
  4. Replies: 3
    Last Post: 03-13-2012, 12:15 PM
  5. function pointer issue
    By drshmoo in forum C Programming
    Replies: 7
    Last Post: 09-22-2011, 03:04 PM

Tags for this Thread