Why is compiler saying that this is not pointer:
#include <stdio.h>
double f(){ return 1.2; }
int main(){
int i=0;
double d =f()[0]; //f() is not pointer? it should be
printf("%i\n",d);
}
error:
subscripted value is neither array nor pointer nor vector
double d =f()[0];
But is I have declared a function pointer, and then used a name of a function, then it will become pointer:
#include <stdio.h>
int op(int (*op)(int,int), int a, int b){ return op(a,b); }
int add(int a, int b){ return a+b; }
int main(){
printf("%i\n",op(add, 1, 2)); //here: add() will magically become pointer, different context
}
So in first case, I wanted to dereference the function, in hope, the name of the funtion is pointer (and so derefencing is permitted). In second example, the function pointer is declared with pointer, so the function add
will decay to pointer (compare to printf("%i\n",op(&add,1,2))
) will also work. So why is problem with the first one?