I have some confusion about reading the value of a function pointer variable.
#include <stdio.h>
char* cat_sound()
{
return "meow";
}
char* dog_sound()
{
return "hav";
}
typedef char* (*fptr_sound)(void);
typedef struct
{
int age;
fptr_sound sound;
}creature_t;
int main()
{
creature_t cat, dog;
cat.sound = cat_sound;
dog.sound = dog_sound;
printf("cat.sound()= %s, dog.sound()= %s\n", cat.sound(), dog.sound());
printf("cat.sound= %s, dog.sound= %s\n", cat.sound, dog.sound);
printf("*cat.sound= %s, *dog.sound= %s\n", *cat.sound, *dog.sound);
printf("*cat.sound()= %c, *dog.sound()= %c\n", *cat.sound(), *dog.sound());
printf("value of cat.sound= %x, dog.sound= %x\n", cat.sound, dog.sound);
printf("value of *cat.sound= %x, *dog.sound= %x\n", *cat.sound, *dog.sound);
printf("value of cat.sound()= %x, dog.sound()= %x\n", cat.sound(), dog.sound());
printf("value of *cat.sound()= %x, *dog.sound()= %x\n", *cat.sound(), *dog.sound());
return 0;
}
cat.sound()= meow, dog.sound()= hav
cat.sound= U▒▒]▒U▒▒]▒U▒▒S▒▒▒▒ ▒D$▒D$&▒D$▒ЉËD$▒Љ\▒D$▒$▒▒▒▒▒T$▒D$▒T▒D$▒$▒x▒▒▒▒T$▒D$▒T▒D$▒$<▒\▒▒▒▒D$▒▒▒, dog.sound= U▒▒]▒U▒▒S▒▒▒▒ ▒D$▒D$&▒D$▒ЉËD$▒Љ\▒D$▒$▒▒▒▒▒T$▒D$▒T▒D$▒$▒x▒▒▒▒T$▒D$▒T▒D$▒$<▒\▒▒▒▒D$▒▒▒
*cat.sound= U▒▒]▒U▒▒]▒U▒▒S▒▒▒▒ ▒D$▒D$&▒D$▒ЉËD$▒Љ\▒D$▒$▒▒▒▒▒T$▒D$▒T▒D$▒$▒x▒▒▒▒T$▒D$▒T▒D$▒$<▒\▒▒▒▒D$▒▒▒, *dog.sound= U▒▒]▒U▒▒S▒▒▒▒ ▒D$▒D$&▒D$▒ЉËD$▒Љ\▒D$▒$▒▒▒▒▒T$▒D$▒T▒D$▒$▒x▒▒▒▒T$▒D$▒T▒D$▒$<▒\▒▒▒▒D$▒▒▒
*cat.sound()= m, *dog.sound()= h
value of cat.sound= 804841c, dog.sound= 8048426
value of *cat.sound= 804841c, *dog.sound= 8048426
value of cat.sound()= 80485f0, dog.sound()= 80485f5
value of *cat.sound()= 6d, *dog.sound()= 68
As seen below, cat.sound holds the address of cat_sound function. So how come *cat.sound returns the same value? (If comparing to a pointer to integer, de-referencing returns the value of to which it points rather than the value itself.)
And why does cat.sound() point to the value returned from the function rather than the function address itself?
08048430 <main>:
8048430: 55 push ebp
8048431: 89 e5 mov ebp,esp
8048433: 53 push ebx
8048434: 83 e4 f0 and esp,0xfffffff0
8048437: 83 ec 20 sub esp,0x20
804843a: c7 44 24 1c 1c 84 04 mov DWORD PTR [esp+0x1c],0x804841c
0804841c <cat_sound>:
804841c: 55 push ebp
804841d: 89 e5 mov ebp,esp
804841f: b8 f0 85 04 08 mov eax,0x80485f0
8048424: 5d pop ebp
8048425: c3 ret
08048426 <dog_sound>:
8048426: 55 push ebp
8048427: 89 e5 mov ebp,esp
8048429: b8 f5 85 04 08 mov eax,0x80485f5
804842e: 5d pop ebp
804842f: c3 ret