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

  • SEARCH
  • Home
  • 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 7609051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:04:39+00:00 2026-05-31T01:04:39+00:00

Hi I am trying to learn some function pointers in C/C++ and I was

  • 0

Hi I am trying to learn some function pointers in C/C++ and I was trying to write the following C++ code with gcc on Ubuntu.

This code should execute the multiply or or the add function depending on the
preprocessor flag -DADD or -DMULTIPLY provided during compilation

#include <iostream>
#include <iomanip>
//Adds two numbers
int add(int a, int b)
{
  return a+b;
}
//Multiplies two numbers
int multiply(int a, int b)
{
  return a*b;
}

//Function to set the correct function to be executed.
//All functions here should have the same signature.
void functionsetter( void (*ptr2fun)(int,int)  )
{
#ifdef ADD
  ptr2fun   = add;
#endif

#ifdef MULTIPLY 
   ptr2fun  = multiply
#endif
} 

int main(int argc, char *argv[])
{
  int a = 5;
  int b = 6;

  void (*foo)(int,int);
  functionsetter(foo);

   return 0;
}

I cannot figure out how to pass the function pointer foo to the function-setter function by reference. Can someone help me out on this.I am sure the declaration of

functionsetter is wrong in the code, Please let me know how to fix it.

I am trying to compile this with g++ -O2 -g -Wall -DADD main.cpp -o main

Note: I want to use such preprocessor flags and function pointers in some other-code elsewhere.
Please let me know if such a thing is a good idea / practice.

  • 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-31T01:04:41+00:00Added an answer on May 31, 2026 at 1:04 am

    Use a typedef:

    typedef void (*MyFunctionPointer)(int,int);
    void functionsetter(MyFunctionPointer& fp);
    

    I want to use such preprocessor flags and function pointers in some other-code elsewhere. Please let me know if such a thing is a good idea / practice.

    No, not really. It isn’t clear from your example what you are trying to accomplish, but your implementation is rather unusual. Consider using virtual member functions or std::function to switch function implementations at runtime, or (possibly) templates to switch them at compile-time. There’s nothing wrong with using conditional compilation for static selection like this, but mixing that with function pointers is a bit odd.

    Without a good understanding of the problem you are trying to solve, it’s difficult to give good advice as to how best to solve it.

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

Sidebar

Related Questions

I'm currently trying to learn OO Javascript so I can write some cleaner code.
jQuery(function ($) {..} I am trying to learn jQuery and came across some code
I'm trying to learn jquery, I've some basics of javascript. I've written this piece
I'm trying to learn some OpenGL via this tutorial . My question is regarding
So I'm trying to learn SQLCLR and have chosen to write a table-valued function
Im trying to learn javascript by tracing through some code at the moment, I
I'm a C# developer who is trying to learn some AS3, so this is
I am trying to learn C. Reading through some code, I came across a
I'm trying to learn some Template Haskell. As an exercise, I wrote a function
I'm trying to learn some Template Haskell. As an exercise, I wrote a function

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.