Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5948857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:10:05+00:00 2026-05-22T17:10:05+00:00

I am studying a C++ tutorial. I can’t understand this example on Pointers to

  • 0

I am studying a C++ tutorial. I can’t understand this example on Pointers to Functions.
Here it is:-

// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
  int g;
  g = (*functocall)(x,y);
  return (g);
}

int main ()
{
  int m,n;
  int (*minus)(int,int) = subtraction;

  m = operation (7, 5, addition);
  n = operation (20, m, minus);
  cout <<n;
  return 0;
}

The lines “m = operation (7, 5, addition);” and “n = operation (20, m, minus);” are treated the same way, but while minus has been declared as a pointer to function, addition hasn’t. So, how did they both work the same way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:10:05+00:00Added an answer on May 22, 2026 at 5:10 pm

    The type of addition is int (&)(int,int) which can decay into a pointer of type int (*)(int,int) which is same as that of operation function’s third parameter. So you can pass addition as third argument to the function operation.

    The type of subtraction is also the same as that of addition. In your code, the address of subtraction is first stored as local variable of the compatible type, and then that variable is passed as argument to operation function.

    In case of addition, it’s address is not stored as local variable, instead its passed as such to operation. Its initializing the function’s third parameter directly with the function’s address, without using any local variable.

    A conversion from int (&)(int,int) to int (*)(int,int) occurs in both cases. Its just that with substration, the conversion occurs when initializing the local variable, and with addition, the conversion occurs when initializing the function parameter.

    An analogy would be this:

    void f(double a, double b) {}
    
    int main()
    {
       double x = 100;//first store 100 in a local variable
       f(x, 100);  //pass the local variable as first arg, 
                   //and pass 100 as second arg without using any local variable.
    }
    

    Note the type of 100 is int, so it first converts to double type, which is then stored as local variable x, which in turn is passed to the function f as first argument. And the second argument 100 is passed directly to the function, so even now it first converts to double and then it initializes b (the second parameter of the function).

    Again, a conversion from int to double occurs in both cases. Its just that first argument conversion occurs when initializing the local variable x, and second argument conversion occurs when initializing the second parameter of the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently studying this tutorial : and I understand completely how to rotate/scale/translate each
I'm studying jQuery with this tutorial, but one of examples doesn't work. <html> <head>
I've been studying how to use anti-alias in OpenGL. Here is a tutorial I
After studying TCP/UDP difference all week, I just can't decide which to use. I
Since I started studying object-oriented programming, I frequently read articles/blogs saying functions are better,
Can any one suggest good tutorials for studying elgg, for elgg beginners
While studying for the SCJP 6 exam, I ran into this question in a
After studying Hadley's book and searching here on SO I have created a heatmap
Im studying some ruby code and I see this varx variable being used with
While studying a software methodologies, I often see disclaimers along the lines of This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.