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 3669948
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:18:18+00:00 2026-05-19T02:18:18+00:00

Take the following C/C++ code: #include <stdlib.h> int inc(int i) { return i+1; }

  • 0

Take the following C/C++ code:

#include <stdlib.h>

int inc(int i) { return i+1; }  // int→int, like abs()
// baz is bool→(int→int)
int (*baz(bool b))(int) { return b ? &abs : &inc; }

int main() {
  int (*foo(bool))(int);  // foo is &(bool→(int→int))
  foo = baz;
}

Attempting to compile this (gcc or g++) gives:

$ g++ test.cc
test.cc: In function ‘int main()’:
test.cc:9: error: assignment of function ‘int (* foo(bool))(int)’
test.cc:9: error: cannot convert ‘int (*(bool))(int)’ to ‘int (*(bool))(int)’ in assignment

Check for yourself: the two types it claims it cannot convert between are exactly the same. Why then is it claiming that they are incompatible?

EDIT 1: The problem disappears when using typedefs (as is recommended), like so:

int main() {
  typedef int (*int2int)(int);
  typedef int2int (*bool2_int2int)(bool);
  bool2_int2int foo;
  foo = baz;
}

EDIT 2: The compiler, of course, was right. The problem with my original code, as many pointed out, is that foo in main() is a declaration of a function, and not a function pointer. The error in the assignment was therefore not conflicting types but assigning to a function, which is not possible. The correct code is:

#include <stdlib.h>

int inc(int i) { return i+1; }  // int→int, like abs()
// baz is bool→(int→int)
int (*baz(bool b))(int) { return b ? &abs : &inc; }

int main() {
  int (*(*foo)(bool))(int);  // foo is &(bool→(int→int))
  foo = &baz;
}
  • 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-19T02:18:19+00:00Added an answer on May 19, 2026 at 2:18 am

    The code is in fact wrong. The problem is that this line:

    int (*foo(bool))(int);  // foo is &(bool→(int→int))
    

    … doesn’t mean what you think it means. It’s interpreted as a declaration of a function named “foo”. That makes perfect sense. Think about it – if you had wanted to forward declare “baz”, you would have put int (*baz(bool))(int); , right? Also, since baz is a function which returns a function pointer, and foo is a pointer to a function which returns a function pointer, wouldn’t you expect the syntax to be more complicated?

    You declared foo as a function of the same type as baz, rather than as a pointer to a function of same type as baz.

    From your compiler, the first error message is the useful one – it tells you assignment of function, i.e. you have tried to assign to a function, which is an error.

    I’m not even going to try to write the correct solution without typedefs 🙂 Here’s some code which compiles and I think is right, using typedefs:

    #include <stdlib.h>
    #include <stdbool.h>
    
    typedef int(*IntReturnsInt)(int);
    
    int inc(int i) { return i+1; } 
    IntReturnsInt baz(bool b) { return b ? &abs : &inc; }
    
    int main() {
      IntReturnsInt (*foo)(bool b);
      foo = baz;
    }
    

    In this example the double-function-pointer concept is a bit clearer – IntReturnsInt is a function pointer type and foo is a pointer to a function which returns IntReturnsInt… phew 🙂

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

Sidebar

Related Questions

Take the following two lines of code: for (int i = 0; i <
Please take a look at following code: #include <stdio.h> #include <iostream> using namespace std;
Take the following code for example; if (Convert.ToString(frm.WindowState) == Minimized) Layout.WindowState = Maximized; else
I'm trying to write a piece of code that will do the following: Take
Take the following snippet: List<int> distances = new List<int>(); Was the redundancy intended by
Take the following function: DataTable go() { return someTableAdapter.getSomeData(); } When I set a
Take the following class as an example: class Sometype { int someValue; public Sometype(int
Consider the following code: #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <algorithm>
I know that to create a file in c++ we use following code #include
I use the following piece of code in an include file. Because it it

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.