Thread: calling functions using pointers insteded of function names

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    73

    calling functions using pointers insteded of function names

    I don't understand how a function pointer is used to call any function

    I have written a program in which i have created four functons

    Code:
     #include <stdlib.h>
    
    void function1 ()
    {
       printf("\n Hello ");	
    }
    
    
    void function2 ( int x)
    {
    	x = 2; 
    	printf("\n x = %d ");	
    }
    
    
    int function3 ( int y)
    {
    	y = 2; 
    	y++;
    	return y;	
    }
    
    
    int function4 ()
    {
    	z = 2; 
    	z++;
    	return z;	
    }
    
    
    int main ()
    {
    	
    return 0;	
    }
    I want to call each functions using pointers insteded of function names but I don't understand how to do it. mainly I am having trouble in type casting

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Code:
    #include <stdio.h>
     
    void function1()      { printf("Hello\n"); }
    void function2(int x) { printf("x = %d\n", x); }
    int  function3(int y) { return y + 1; }
    int  function4()      { return 42; }
     
    int main ()
    {
        void (*f1)()    = function1;
        void (*f2)(int) = function2;
        int  (*f3)(int) = function3;
        int  (*f4)()    = function4;
     
        f1();
        f2(f3(f4()));
     
        return 0;   
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    73
    Quote Originally Posted by john.c View Post
    Code:
    #include <stdio.h>
     
    void function1()      { printf("Hello\n"); }
    void function2(int x) { printf("x = %d\n", x); }
    int  function3(int y) { return y + 1; }
    int  function4()      { return 42; }
     
    int main ()
    {
        void (*f1)()    = function1;
        void (*f2)(int) = function2;
        int  (*f3)(int) = function3;
        int  (*f4)()    = function4;
     
        f1();
        f2(f3(f4()));
     
        return 0;   
    }
    understood, Thanks for the effort

    Code:
    #include <stdio.h>  
    void function1()      { printf("Hello\n"); }
    void function2(int x) { printf("x = %d\n", x); }
    int  function3(int y) { return y + 1; }
    int  function4()      { return 42; }
      
    int main ()
    {
    	int a, b;
        void (*f1)()    = function1;
        void (*f2)(int) = function2;
        int  (*f3)(int) = function3;
        int  (*f4)()    = function4;
      
        a = f3(2);
        printf("a = %d\n", a); 
        b = f4();
    	printf("b = %d\n", b); 
        return 0;   
    }
    a = 3
    b = 42

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-23-2019, 11:30 PM
  2. Replies: 1
    Last Post: 12-15-2017, 08:01 PM
  3. Using Variable Names When Calling a MC PIN High or Low
    By C-Noob#1 in forum C Programming
    Replies: 2
    Last Post: 02-10-2013, 03:07 AM
  4. Replies: 7
    Last Post: 01-12-2013, 05:07 PM
  5. Replies: 5
    Last Post: 01-13-2006, 12:00 AM

Tags for this Thread