### function_pointer_1

Hello.
Assuming the following definitions:
 ``123`` ``````int add(int a, int b) {return a + b;} int (*op)(int a, int b)= add;``````

Why is the call

wrong after the line
 int (*op)(int a, int b)= add;
?

`*op = add + 1;` ¿what do you think you are doing there?
I am after the reason for the syntax error.All of them.
Last edited on
You dereference a function pointer, and then try to assign it to a function (ie. the address of that function) plus one? This makes no sense. What are you trying to accomplish?
I now look for the reqasons for the error.
Last edited on
You can't do pointer arithmetic with a function's address.
Also, I believe dereferencing a function pointer will just give you the address right back.
 ``1234567`` ``````*p; **p; ***p; ****p; //They all do the exact same thing. //You can do this too: int a = (******p)(1,1);``````
> I now look for the reqasons for the error.

The binary + operator is available only for pointers that point to a complete object.
In particular. `T* pointer ; pointer + 1 ;` requires that `T` is a complete type - `sizeof(T`) is known at compile-time.

A function is not an object.

 ``123456789101112131415161718192021`` ``````struct A ; int main() { typedef int function_type( int, int ) ; function_type* p1 /* = ... */ ; void* p2 /* = ... */ A* p3 /* = ... */ sizeof( function_type ) ; // *** error - not an object sizeof( void ) ; // *** error - void is deemed to be an incomplete type sizeof( A ) ; // *** error - A is an incomplete type function_type a1[1] ; // *** error void a2[1] ; // *** error A a3[1] ; // *** error - A is an incomplete type p1 + 1 ; // *** error - p1 does not point to an object p2 + 1 ; // *** error - p2 does not point to a complete object p3 + 1 ; // *** error - p3 does not point to a complete object }``````
Topic archived. No new replies allowed.