Can't understand pointer operator
fp
- pointer
(*fp)
to function
(*fp)(
which takes 1 argument like char
(*fp)(char)
and returns a value like int
int (*fp)(char)
The pointer is initialized with an address puts
after a major redundant conversion.
int (*fp)(char *)=(int(*)(char *))&puts
int (*fp)(char *)=(int(*)(char *))puts // & redundant
int (*fp)(const char *)=puts
The object is i
not initialized. It is of typeint
int (*fp)(char *)=(int(*)(char *))&puts, i;
source to share
First, the variable declaration appears:
int (*fp)(char *)
fp
is a pointer to a function that takes a parameter char *
and returns int
.
Then it is fp
initialized with a value:
(int(*)(char *))&puts
The value is the address of the function puts
, passed in the same type as fp
.
And finally, there is another variable declaration:
int /* ... */, i;
source to share
The ad has two parts:
int (*fp)(char *)=(int(*)(char *))&puts, i;
first : int (*fp)(char *)=(int(*)(char *))&puts;
Explanation: This is a function pointer declaration and initialization in one expression. Where fp
is a function pointer puts
. If you print the values fp
and puts
, they will have the same meaning, that is, the address puts
.
#include<stdio.h>
int main()
{
int (*fp)(char *)=(int(*)(char *))&puts, i;
printf("puts %p\n",puts);
printf("fp %p\n",fp);
}
and the second one :int i;
source to share