Thread: Select Menu and Call Function

  1. #16
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    Re: here it might help

    >>>>>you can just use an array of function pointers and use the number input by the user as the index in the array<<<

    Can you give an example of this?

    Thanks!

  2. #17
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void func1( int);
    void func2( int );
    
    int main()
    {
     void (*f[2])(int) = { func1, func2 };
     int choice;
    
     printf("Enter an input:");
     scanf("%d", &choice );
    
     (*f[ choice ])(choice);
          system("PAUSE");
          return 0;
    }
    void func1( int num)
    {
     printf("you entered %d.So function 1 was called\n", num);
    }
    void func2( int num)
    {
     printf("You enterd %d.SO function2 was vcalled", num );
    }
    This ia an example for array of pointer to a function.A question for
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  3. #18
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by datainjector
    Code:
     (*f[ choice ])(choice);
    Actually, you can do

    f[choice]( choice );

    You don't have to explicitly dereference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-27-2009, 12:46 PM
  2. Data Structures Warning
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 04-08-2006, 12:04 AM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  4. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 06:16 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM