What is a function name? What's its relation to a pointer to it?To try to understand these questions, codes below are written:
#include <stdio.h>
int testFunc(void);
void ptrFuncProp(void);
int main(){
ptrFuncProp();
return 0;
}
void ptrFuncProp(void){
int i = 0;
int (*p_testFunc)(void) = testFunc;
testFunc();
(*testFunc)();
p_testFunc();
(*p_testFunc)();
printf("testFunc:\t%d\n*testFunc:\t%d\n",sizeof(testFunc),sizeof(*testFunc));
printf("p_testFunc:\t%d\n*p_testFunc:\t%d\n",sizeof(p_testFunc),sizeof(*p_testFunc));
putchar('\n');
printf("testFunc:\t%c\n",testFunc);
printf("*testFunc:\t%c\n",*testFunc);
printf("*p_testFunc:\t%c\n",*p_testFunc);
for(;*p_testFunc && i<30;i++){
printf("%c ",*(p_testFunc + i));
if(i%10 == 9){
putchar('\n');
}
}
}
int testFunc(void){
int i=0;
printf("output by testFunc\n");
return 0;
}
The output is as follows:
In the code, a simple function testFunc is defined, and a pointer p_testFunc points to it.As I learned on the internet, I tried four ways to call this function;they all work although I don't exactly understand.
Next 2 lines try to figure out what really are the function name and a pointer of it.One thing I can understand is that p_testFunc is a pointer, so it contains the address of something else;the address is 8 bytes. But why the size of the function name is 1 bytes because I used to think a function name is a const pointer whose content is the address of the start of the function. If the function name is not a pointer, how can it be dereferenced?
After the experiment, the questions are still unsolved.