neel
neel

Reputation: 9061

why are all outputs the same in this C program?

#include<stdio.h>
int main(){
    extern void fun(int);
    void (*p)(int) = fun;
        fun(2);
    (*fun)(2);
    (*p)(2);
    p(2);
    printf("%x %x %x\n",p,fun,*fun);
}

void fun(int i){
    printf("hi %d\n",i);
}  

Here all function calls are giving the same output. And even p, fun, *fun are giving the same address. How can we interpret this?

How can fun and *fun be same?

Upvotes: 4

Views: 225

Answers (1)

ouah
ouah

Reputation: 145829

Because C says:

(C99, 6.5.3.2p4) "The unary * operator denotes indirection. If the operand points to a function, the result is a function designator;"

fun and *fun have the same value as they are equivalent.

Upvotes: 6

Related Questions