In C, assigning a function pointer to the corresponding typed variable gives you "cannot convert ... to assignment",

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; } 

Trying 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 that he claims that they cannot convert among themselves are exactly the same. Why does he claim that they are incompatible?

EDIT 1 . The problem disappears when using typedef (as recommended), for example:

 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 source code, as many have pointed out, is that foo in main() is a function declaration, not a function pointer. Consequently, the error in the assignment did not contradict the types, but assigned a function, which is impossible. 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; } 
+4
source share
5 answers

The code is actually wrong. The problem is that this line:

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

... does not mean what you think it means. It is interpreted as declaring a function named "foo". It makes sense. Think about it - if you wanted to send a "baz" declaration, you would put int (*baz(bool))(int); , right? Also, since baz is a function that returns a pointer to a function, and foo is a pointer to a function that returns a pointer to a function, do you expect the syntax to be more complicated?

You declared foo as a function of the same type as baz, and not as a pointer to a function of the same type as baz.

From your compiler, the first error message is useful: it tells you the assignment of function , i.e. You tried to assign a function that is an error.

I'm not even going to write the right solution without typedefs :-) Here is some code that compiles, and I think it's 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 concept with two functional pointers is a little clear - IntReturnsInt is the type of the function pointer, and foo is the pointer to the function that returns IntReturnsInt ... phew :-)

+7
source

This is a feature announcement.

 int (*foo(bool))(int); 

If you want to declare a pointer to a function, you must do:

 int (*(*foo)(bool))(int); 
+3
source
 #include <stdlib.h> #include <stdbool.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; return 0; } 

Thus, there were several side issues that clouded the underlying problem. Your "bool" is interpreted as an untyped default parameter - int , because the actual built-in _Bool, humanized by the previously missing #include <stdbool.h> . The absence of a pointer declaration for an object in the stack displaced its ability to correspond to the type of a real function object in static memory a little higher.

As soon as I turned on <stdbool.h> , the error shifted to the "lvalue required" complaint because there was only a function declaration, not a pointer object. The above code will compile without warning or error.

0
source

It's hard to be sure, but I think this is closer to the OP's intention:

 // baz is a function returning a pointer to a function int (*baz(bool b))(int) { return b ? &abs : &inc; } int main() { // foo is a pointer to a function int (*foo)(int) ; foo = baz(true); // Now foo is equal to &abs } 
0
source

you cannot assign a function type ( int (*foo(bool))(int); ), you need to use a function pointer

 int (*(*foo)(bool))(int); foo = &baz; 
0
source

Source: https://habr.com/ru/post/1334906/


All Articles