Thread: Function pointer problem

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    56

    Function pointer problem

    Hi

    What's going wrong with code that is why printf is not printing X

    Code:
    #include<stdio.h>
    
    void foo (void )
    {
    	printf(" X");
    }
    int main(void)
    {
        void (*Fp)(void) = &foo;
        (*Fp) ;   
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,194
    You aren't calling the function, just "mentioning" the function name, which does nothing.
    Code:
    #include <stdio.h>
     
    void foo()
    {
        printf("X\n");
    }
     
    int main()
    {
        void (*f)(void) = &foo; // don't actually need the &
     
        (*f)();  // the parens that contain the arguments (if any) constitute
                 // the "function call operator"
     
        f();     // and you don't actually need the dereference operator
     
        return 0;
    }
    We live as it were by chance, and by chance we are governed. - Seneca

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Pointer Problem
    By YlmzCmlttn in forum C Programming
    Replies: 1
    Last Post: 03-22-2017, 05:46 PM
  2. Problem with function pointer
    By XioNOverMazes in forum C++ Programming
    Replies: 1
    Last Post: 05-13-2015, 04:50 AM
  3. Function and Pointer problem
    By austin.18 in forum C Programming
    Replies: 4
    Last Post: 11-03-2014, 11:16 AM
  4. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 04:50 AM
  5. Replies: 4
    Last Post: 11-05-2006, 02:57 PM

Tags for this Thread