Why is a function pointer
behaving like an array pointer
as far as this behavior goes?I mean, let's begin with the case of an array list[]
where we'll consider &list
and list
.
char name[5]= "Eric",(*aptr)[5]=&name;
printf("%p,%p",*aptr,name); //BOTH ARE NUMERICALLY SAME
and we can refer to array elements also as (*aptr)[1]
,(*aptr)[2]
, etc.I understand what's going on here.
But why is the same working for functions?After all a "function" as such is not a contiguous memory block of similar elements as an array is.Consider this.
Suppose fptr
is a function pointer as in my program.Why does fptr
and *fptr
give the same value when printed?What does *fptr
even mean?I only knew that we can invoke a function using its pointer as (*fptr)()
or as fptr()
,but what is *fptr
alone then?
#include<stdio.h>
void foo(){};
int main(void)
{
void (*fptr)()=foo;
printf("%p,%p",fptr,*fptr);
}
Result- 00401318 00401318