how do i implement Pointers to call the function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Virus2030
    New Member
    • Mar 2014
    • 1

    how do i implement Pointers to call the function

    write a program which contains two global variables:
    1.Account number (integer)
    2.Account Balance (float)
    and three functions :
    A function called set values which sets initial values for account number and account balance,
    A function called set values which prompt user to enter the values of the above variables,
    A function called input transaction which reads a character value for transaction type that is D (for deposit) and W (for withdrawal ) and a floating point value for transaction amount which updates the account balance .
    Implement a pointer to call each of the functions using C++.


    thank you guys for your consideration .
    Last edited by Virus2030; Mar 11 '14, 03:28 PM. Reason: specifying the programming language
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The easiest way to define a function pointer is to start with the function prototype:

    Code:
    int MyFunction(int arg1, int* arg2);
    Then replace the function name with the name of the function pointer preceded by an asterisk and surrounded with paretheses. So if the pointer name is ptr then:

    Code:
    int (*ptr)(int arg1, int* arg2);
    is the function pointer.

    Next, remember that the name of a function is the address of the function, so you then:

    Code:
    ptr = MyFunction;
    This puts the address of MyFunction into the pointer. You then use ptr to call the function:

    Code:
    ptr(25, &something);
    ptr can be used to call any function that has an int and an int* for arguments and returns an int.

    Comment

    Working...