Reg Fucntion pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sowmi
    New Member
    • Jun 2007
    • 3

    Reg Fucntion pointers

    Hi All,

    I'm learning Pointers to functions in C.But I have some doubts regardign the same.I would be v.happy and thankful if somebody can help me out on my pbm
    The problem is mentioned below:
    ------------------------------------------
    Consider I have 5 different functions which returns different type and takes different arguements also.
    Eg :
    int aaa(int x);
    void bbb(int x,int y, int z);
    void ccc(void);
    void ddd(char *x,char *y,char *z);
    void eee(UINT8 x);

    And these are all some operations when the user presses some commands,
    Eg:
    Commands = "command1","com mand2","command 3","command4"," command5"

    How will i use the fucntion pointers to execute the corresponding function with their arguements?

    If possible, if somebody can reply to me ASAP.

    Thanks in advance,
    Sowmi
  • askcq
    New Member
    • Mar 2007
    • 63

    #2
    i didnt get "pressing commands "
    can u explain it little more clearly

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by sowmi
      int aaa(int x);
      void bbb(int x,int y, int z);
      void ccc(void);
      void ddd(char *x,char *y,char *z);
      void eee(UINT8 x);
      These functions will need different function pointers.
      [code=cpp]
      int aaa(int x);\
      [/code]

      will need a function pointer:
      [code=cpp]
      int (*fp)(int);
      [/code]
      You assign the address of the functtion to the pointer:
      [code=cpp]
      fp = aaa;
      [/code]
      and then you can call the function using the pointer:
      [code=cpp]
      fp(20);
      [/code]
      But you need a diffrerent function pointer for:
      [code=cpp]
      void bbb(int x,int y, int z);

      void (*fp1)(int, int, int);

      fp1 = bbb;

      fp1(10,20,30);
      [/code]
      Your example is not a case where you use function pointers. Function pointers are used where you have many functions with the same arguments and the same return type. Then you can choose which function to call by putting its address in a function pointer. The pointer can be passed to another function so that the other function can call different functions. This is commonly done with sorts where you pass the address of the compare function to the sort so you can sort in different sequences.

      Comment

      Working...