Select Menu and Call Function

This is a discussion on Select Menu and Call Function within the C Programming forums, part of the General Programming Boards category; >>>>>you can just use an array of function pointers and use the number input by the user as the index ...

  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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-27-2009, 11:46 AM
  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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21