Passing a pointer to a function as an argument to a functionIs Java “pass-by-reference” or “pass-by-value”?What are the differences between a pointer variable and a reference variable in C++?What is a smart pointer and when should I use one?How do function pointers in C work?How do I pass a variable by reference?Why do we need virtual functions in C++?How to pass all arguments passed to my bash script to a function of mine?Check number of arguments passed to a Bash scriptWhy should I use a pointer rather than the object itself?Passing capturing lambda as function pointer

Should I put my name first or last in the team members list?

Has the US government provided details on plans to deal with AIDS and childhood cancer?

Why are sugars in whole fruits not digested the same way sugars in juice are?

What are the effects of the elements on 3D printed objects made with "infused" PLA

Error with uppercase in titlesec's label field

Security measures that could plausibly last 150+ years?

Does the problem of P vs NP come under the category of Operational Research?

Went to a big 4 but got fired for underperformance in a year recently - Now every one thinks I'm pro - How to balance expectations?

Word for giving preference to the oldest child

Best Ergonomic Design for a handheld ranged weapon

How can a class have multiple methods without breaking the single responsibility principle

Normally Closed Optoisolators

What is my clock telling me to do?

Conflict between senior and junior members

How to structure presentation to avoid getting questions that will be answered later in the presentation?

Can birds evolve without trees?

Return last number in sub-sequences in a list of integers

Not taking Bereavement Leave

Can machine learning learn a function like finding maximum from a list?

Should 2FA be enabled on service accounts?

Adjective for when skills are not improving and I'm depressed about it

A game of red and black

Feedback diagram

Why have both: BJT and FET transistors on IC output?



Passing a pointer to a function as an argument to a function


Is Java “pass-by-reference” or “pass-by-value”?What are the differences between a pointer variable and a reference variable in C++?What is a smart pointer and when should I use one?How do function pointers in C work?How do I pass a variable by reference?Why do we need virtual functions in C++?How to pass all arguments passed to my bash script to a function of mine?Check number of arguments passed to a Bash scriptWhy should I use a pointer rather than the object itself?Passing capturing lambda as function pointer






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Just wondering if anyone can give me some advice regarding where I'm going wrong here. My program works OK if I run it as is, but as soon as I swap the commented line with the one below it, I get errors. My goal is to be able to use the commented line because I want to create a program which let's me pass a pointer to a function as an argument to another function, but so far I'm having no luck.



#include <iostream>
using namespace std;

double arith_op(double left, double right, double (*f)(double, double));
double addition(double left, double right);
double subtraction(double left, double right);
double multiplication(double left, double right);

int main()

  double left, right;
  int choice;
  double (*f[3])(double, double) = addition, subtraction, multiplication ;
  
  cout << "Enter 1 for addition, 2 for subtraction, 3 for multiplication "
       << "(-1 to end): " << endl;
  cin >> choice;

  while (choice != -1)

    cout << "Enter a floating-point number: " << endl;
    cin >> left;

    cout << "Enter another floating-point number: " << endl;
    cin >> right;

    // double* result = arith_op(left, right, f[choice - 1](left, right));
     double result = f[choice - 1](left, right);

    if (choice == 1)
      cout << left << " + " << right << " = " << result;
   
    else if (choice == 2)
      cout << left << " - " << right << " = " << result;    
   
    else
      cout << left << " * " << right << " = " << result;    
   
    cout << endl;

    cout << "Enter 1 for addition, 2 for subtraction, 3 for multiplication "
     << "(-1 to end): " << endl;
    cin >> choice;
 


double arith_op(double left, double right, double (*f)(double, double))

  return (*f)(left, right);


double addition(double left, double right)

  return left + right;

double subtraction(double left, double right)

  return left - right;


double multiplication(double left, double right)

  return left * right;



I should add that my end goal is to package the function arith_op and the other functions in a seperate file, then use them by including their prototypes with 'extern'. This may be an odd way to approach the problem - it's for an assignment, and they're always odd.



Thank you :)



Wade










share|improve this question



















  • 1





    What type of errors do you get? Besides of that your program is C-style, but not C++ style: You would use classes and inheritance...

    – U. Windl
    Mar 26 at 23:35












  • I've just finished a C course, so you are correct. The structure of the new course I'm doing is such that we are reviewing C first, then moving on to C++ styles. Consequently we aren't allowed to use any OOP paradigms to solve this problem.

    – Wade Shiell
    Mar 27 at 0:05

















0















Just wondering if anyone can give me some advice regarding where I'm going wrong here. My program works OK if I run it as is, but as soon as I swap the commented line with the one below it, I get errors. My goal is to be able to use the commented line because I want to create a program which let's me pass a pointer to a function as an argument to another function, but so far I'm having no luck.



#include <iostream>
using namespace std;

double arith_op(double left, double right, double (*f)(double, double));
double addition(double left, double right);
double subtraction(double left, double right);
double multiplication(double left, double right);

int main()

  double left, right;
  int choice;
  double (*f[3])(double, double) = addition, subtraction, multiplication ;
  
  cout << "Enter 1 for addition, 2 for subtraction, 3 for multiplication "
       << "(-1 to end): " << endl;
  cin >> choice;

  while (choice != -1)

    cout << "Enter a floating-point number: " << endl;
    cin >> left;

    cout << "Enter another floating-point number: " << endl;
    cin >> right;

    // double* result = arith_op(left, right, f[choice - 1](left, right));
     double result = f[choice - 1](left, right);

    if (choice == 1)
      cout << left << " + " << right << " = " << result;
   
    else if (choice == 2)
      cout << left << " - " << right << " = " << result;    
   
    else
      cout << left << " * " << right << " = " << result;    
   
    cout << endl;

    cout << "Enter 1 for addition, 2 for subtraction, 3 for multiplication "
     << "(-1 to end): " << endl;
    cin >> choice;
 


double arith_op(double left, double right, double (*f)(double, double))

  return (*f)(left, right);


double addition(double left, double right)

  return left + right;

double subtraction(double left, double right)

  return left - right;


double multiplication(double left, double right)

  return left * right;



I should add that my end goal is to package the function arith_op and the other functions in a seperate file, then use them by including their prototypes with 'extern'. This may be an odd way to approach the problem - it's for an assignment, and they're always odd.



Thank you :)



Wade










share|improve this question



















  • 1





    What type of errors do you get? Besides of that your program is C-style, but not C++ style: You would use classes and inheritance...

    – U. Windl
    Mar 26 at 23:35












  • I've just finished a C course, so you are correct. The structure of the new course I'm doing is such that we are reviewing C first, then moving on to C++ styles. Consequently we aren't allowed to use any OOP paradigms to solve this problem.

    – Wade Shiell
    Mar 27 at 0:05













0












0








0








Just wondering if anyone can give me some advice regarding where I'm going wrong here. My program works OK if I run it as is, but as soon as I swap the commented line with the one below it, I get errors. My goal is to be able to use the commented line because I want to create a program which let's me pass a pointer to a function as an argument to another function, but so far I'm having no luck.



#include <iostream>
using namespace std;

double arith_op(double left, double right, double (*f)(double, double));
double addition(double left, double right);
double subtraction(double left, double right);
double multiplication(double left, double right);

int main()

  double left, right;
  int choice;
  double (*f[3])(double, double) = addition, subtraction, multiplication ;
  
  cout << "Enter 1 for addition, 2 for subtraction, 3 for multiplication "
       << "(-1 to end): " << endl;
  cin >> choice;

  while (choice != -1)

    cout << "Enter a floating-point number: " << endl;
    cin >> left;

    cout << "Enter another floating-point number: " << endl;
    cin >> right;

    // double* result = arith_op(left, right, f[choice - 1](left, right));
     double result = f[choice - 1](left, right);

    if (choice == 1)
      cout << left << " + " << right << " = " << result;
   
    else if (choice == 2)
      cout << left << " - " << right << " = " << result;    
   
    else
      cout << left << " * " << right << " = " << result;    
   
    cout << endl;

    cout << "Enter 1 for addition, 2 for subtraction, 3 for multiplication "
     << "(-1 to end): " << endl;
    cin >> choice;
 


double arith_op(double left, double right, double (*f)(double, double))

  return (*f)(left, right);


double addition(double left, double right)

  return left + right;

double subtraction(double left, double right)

  return left - right;


double multiplication(double left, double right)

  return left * right;



I should add that my end goal is to package the function arith_op and the other functions in a seperate file, then use them by including their prototypes with 'extern'. This may be an odd way to approach the problem - it's for an assignment, and they're always odd.



Thank you :)



Wade










share|improve this question














Just wondering if anyone can give me some advice regarding where I'm going wrong here. My program works OK if I run it as is, but as soon as I swap the commented line with the one below it, I get errors. My goal is to be able to use the commented line because I want to create a program which let's me pass a pointer to a function as an argument to another function, but so far I'm having no luck.



#include <iostream>
using namespace std;

double arith_op(double left, double right, double (*f)(double, double));
double addition(double left, double right);
double subtraction(double left, double right);
double multiplication(double left, double right);

int main()

  double left, right;
  int choice;
  double (*f[3])(double, double) = addition, subtraction, multiplication ;
  
  cout << "Enter 1 for addition, 2 for subtraction, 3 for multiplication "
       << "(-1 to end): " << endl;
  cin >> choice;

  while (choice != -1)

    cout << "Enter a floating-point number: " << endl;
    cin >> left;

    cout << "Enter another floating-point number: " << endl;
    cin >> right;

    // double* result = arith_op(left, right, f[choice - 1](left, right));
     double result = f[choice - 1](left, right);

    if (choice == 1)
      cout << left << " + " << right << " = " << result;
   
    else if (choice == 2)
      cout << left << " - " << right << " = " << result;    
   
    else
      cout << left << " * " << right << " = " << result;    
   
    cout << endl;

    cout << "Enter 1 for addition, 2 for subtraction, 3 for multiplication "
     << "(-1 to end): " << endl;
    cin >> choice;
 


double arith_op(double left, double right, double (*f)(double, double))

  return (*f)(left, right);


double addition(double left, double right)

  return left + right;

double subtraction(double left, double right)

  return left - right;


double multiplication(double left, double right)

  return left * right;



I should add that my end goal is to package the function arith_op and the other functions in a seperate file, then use them by including their prototypes with 'extern'. This may be an odd way to approach the problem - it's for an assignment, and they're always odd.



Thank you :)



Wade







c++ parameter-passing function-pointers extern-c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 23:28









Wade ShiellWade Shiell

163 bronze badges




163 bronze badges










  • 1





    What type of errors do you get? Besides of that your program is C-style, but not C++ style: You would use classes and inheritance...

    – U. Windl
    Mar 26 at 23:35












  • I've just finished a C course, so you are correct. The structure of the new course I'm doing is such that we are reviewing C first, then moving on to C++ styles. Consequently we aren't allowed to use any OOP paradigms to solve this problem.

    – Wade Shiell
    Mar 27 at 0:05












  • 1





    What type of errors do you get? Besides of that your program is C-style, but not C++ style: You would use classes and inheritance...

    – U. Windl
    Mar 26 at 23:35












  • I've just finished a C course, so you are correct. The structure of the new course I'm doing is such that we are reviewing C first, then moving on to C++ styles. Consequently we aren't allowed to use any OOP paradigms to solve this problem.

    – Wade Shiell
    Mar 27 at 0:05







1




1





What type of errors do you get? Besides of that your program is C-style, but not C++ style: You would use classes and inheritance...

– U. Windl
Mar 26 at 23:35






What type of errors do you get? Besides of that your program is C-style, but not C++ style: You would use classes and inheritance...

– U. Windl
Mar 26 at 23:35














I've just finished a C course, so you are correct. The structure of the new course I'm doing is such that we are reviewing C first, then moving on to C++ styles. Consequently we aren't allowed to use any OOP paradigms to solve this problem.

– Wade Shiell
Mar 27 at 0:05





I've just finished a C course, so you are correct. The structure of the new course I'm doing is such that we are reviewing C first, then moving on to C++ styles. Consequently we aren't allowed to use any OOP paradigms to solve this problem.

– Wade Shiell
Mar 27 at 0:05












2 Answers
2






active

oldest

votes


















3














Your problem is in the 3rd argument here



arith_op(left, right, f[choice - 1](left, right));


f[i](left, right) is calling the function to give a double, rather than passing the function pointer to arith_op. Simply remove the parameter list



arith_op(left, right, f[choice - 1]);





share|improve this answer




















  • 3





    Also, on that line the declaration double* result should be double result

    – Chris Taylor
    Mar 26 at 23:34











  • Correct! Thank you sir.

    – Wade Shiell
    Mar 27 at 0:05


















0














smacks head I feel stupid now. It works. Brilliant, thank you!



Sadly my program still doesn't actually get me full marks, but that's because we have to submit it using an automated online marking system. So if your format is not precisely correct, you'll lose marks even if the program actually works fine.



Post script - I also successfully moved my functions to a seperate file, as per the assignment instructions. It's telling me I didn't name my functions correctly, but the program works, which is what matters.






share|improve this answer



























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367634%2fpassing-a-pointer-to-a-function-as-an-argument-to-a-function%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Your problem is in the 3rd argument here



    arith_op(left, right, f[choice - 1](left, right));


    f[i](left, right) is calling the function to give a double, rather than passing the function pointer to arith_op. Simply remove the parameter list



    arith_op(left, right, f[choice - 1]);





    share|improve this answer




















    • 3





      Also, on that line the declaration double* result should be double result

      – Chris Taylor
      Mar 26 at 23:34











    • Correct! Thank you sir.

      – Wade Shiell
      Mar 27 at 0:05















    3














    Your problem is in the 3rd argument here



    arith_op(left, right, f[choice - 1](left, right));


    f[i](left, right) is calling the function to give a double, rather than passing the function pointer to arith_op. Simply remove the parameter list



    arith_op(left, right, f[choice - 1]);





    share|improve this answer




















    • 3





      Also, on that line the declaration double* result should be double result

      – Chris Taylor
      Mar 26 at 23:34











    • Correct! Thank you sir.

      – Wade Shiell
      Mar 27 at 0:05













    3












    3








    3







    Your problem is in the 3rd argument here



    arith_op(left, right, f[choice - 1](left, right));


    f[i](left, right) is calling the function to give a double, rather than passing the function pointer to arith_op. Simply remove the parameter list



    arith_op(left, right, f[choice - 1]);





    share|improve this answer













    Your problem is in the 3rd argument here



    arith_op(left, right, f[choice - 1](left, right));


    f[i](left, right) is calling the function to give a double, rather than passing the function pointer to arith_op. Simply remove the parameter list



    arith_op(left, right, f[choice - 1]);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 26 at 23:31









    Peter BellPeter Bell

    3422 silver badges6 bronze badges




    3422 silver badges6 bronze badges










    • 3





      Also, on that line the declaration double* result should be double result

      – Chris Taylor
      Mar 26 at 23:34











    • Correct! Thank you sir.

      – Wade Shiell
      Mar 27 at 0:05












    • 3





      Also, on that line the declaration double* result should be double result

      – Chris Taylor
      Mar 26 at 23:34











    • Correct! Thank you sir.

      – Wade Shiell
      Mar 27 at 0:05







    3




    3





    Also, on that line the declaration double* result should be double result

    – Chris Taylor
    Mar 26 at 23:34





    Also, on that line the declaration double* result should be double result

    – Chris Taylor
    Mar 26 at 23:34













    Correct! Thank you sir.

    – Wade Shiell
    Mar 27 at 0:05





    Correct! Thank you sir.

    – Wade Shiell
    Mar 27 at 0:05













    0














    smacks head I feel stupid now. It works. Brilliant, thank you!



    Sadly my program still doesn't actually get me full marks, but that's because we have to submit it using an automated online marking system. So if your format is not precisely correct, you'll lose marks even if the program actually works fine.



    Post script - I also successfully moved my functions to a seperate file, as per the assignment instructions. It's telling me I didn't name my functions correctly, but the program works, which is what matters.






    share|improve this answer





























      0














      smacks head I feel stupid now. It works. Brilliant, thank you!



      Sadly my program still doesn't actually get me full marks, but that's because we have to submit it using an automated online marking system. So if your format is not precisely correct, you'll lose marks even if the program actually works fine.



      Post script - I also successfully moved my functions to a seperate file, as per the assignment instructions. It's telling me I didn't name my functions correctly, but the program works, which is what matters.






      share|improve this answer



























        0












        0








        0







        smacks head I feel stupid now. It works. Brilliant, thank you!



        Sadly my program still doesn't actually get me full marks, but that's because we have to submit it using an automated online marking system. So if your format is not precisely correct, you'll lose marks even if the program actually works fine.



        Post script - I also successfully moved my functions to a seperate file, as per the assignment instructions. It's telling me I didn't name my functions correctly, but the program works, which is what matters.






        share|improve this answer













        smacks head I feel stupid now. It works. Brilliant, thank you!



        Sadly my program still doesn't actually get me full marks, but that's because we have to submit it using an automated online marking system. So if your format is not precisely correct, you'll lose marks even if the program actually works fine.



        Post script - I also successfully moved my functions to a seperate file, as per the assignment instructions. It's telling me I didn't name my functions correctly, but the program works, which is what matters.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 0:07









        Wade ShiellWade Shiell

        163 bronze badges




        163 bronze badges






























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367634%2fpassing-a-pointer-to-a-function-as-an-argument-to-a-function%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현