Why are the function pointer, function address and function the same?

#include <stdio.h> int add(int a, int b) { int c =a+b; return c; } int main() { int a=20,b=45; int (*p)(int , int); p=&add; printf("%d\n%d\n%d\n\n",*add,&add,add); printf("%d\n%d\n%d\n\n",*add+1,&add+1,add+1); return 0; } 

Outupt is

4199392 4199392 4199392

4199393 4199393 4199393

So why are * add, & add, add the same? I also doubt that the โ€œaddโ€ act as an array, correct me if I am mistaken, because the address of the array and the array itself.

+5
source share
2 answers

In C, the only thing you can do with the function is to call or accept your address. Therefore, if you do not name it, you largely accept its address.

0
source

"C11 ยง6.5.6 Additive operators" / 6 discusses adding to a pointer

When an expression that has an integer type is added or subtracted from the pointer, the result is the type of the operand of the pointer. If the pointer operand points to an element of an array object, .... If the result indicates one past last element of an array object, ...

Nowhere does C spec define the addition of an integer type to a function pointer. Therefore, the behavior is undefined.

0
source

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


All Articles