-1

In function pointers,

Why is (*fptr)(int a, int b) is same as (fptr)(int a, int b) if the function pointer is assigned to add function?

int (*fptr)(int ,int) = add; 
while add(int a , int b) returns sum of two numbers.    
Ayush
  • 2,608
  • 2
  • 22
  • 31
user259060
  • 121
  • 6
  • 2
    Please post proper code that demonstrates the question. What you have written are two different types (one is a function type, the other is a function pointer type). And the question title is different from the question body. – Kerrek SB Jul 01 '14 at 11:27
  • Now to completely twist your mind: what about `(****fptr)(6, 7)`? – Kerrek SB Jul 01 '14 at 11:29
  • @Ayush: "improved" is a strong word... – Kerrek SB Jul 01 '14 at 11:37
  • You do not need for dereference because function and pointer to function both they are handled as a pointer. – BLUEPIXY Jul 01 '14 at 17:20

2 Answers2

1

Explanation

C doesn't have function objects, so it makes no sense to dereference a function pointer. Therefore, when a function pointer is dereferenced, it is (with some exceptions) immediately turned back into a pointer to function.

References

n1570 (the final public draft of the current C standard):

6.5.3.2 Address and indirection operators

  1. The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; [...]

...

6.3.2.1 Lvalues, arrays, and function designators

  1. A function designator is an expression that has function type. Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, a function designator with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’.
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
0

6.5.2.2 Function calls 5

If the expression that denotes the called function has type pointer to function returning an object type, the function call expression has the same type as that object type, and has the value determined as specified in 6.8.6.4.

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70