Print Function Address

I am trying to figure out how to print the address of a function. Here is what I came up with

#include<stdio.h>
int test(int a, int b)
{
     return a+b;
}
int main(void)
{
     int (*ptr)(int,int);
     ptr=&test;
     printf("The address of the function is =%p\n",ptr);
     printf("The address of the function pointer is =%p\n",&ptr);
     return 0;
}

This is o / p something like this without any warnings and errors

address of function is =0x4006fa
address of function pointer  is =0x7fffae4f73d8

The question is, is using the % p format specifier the right way to print the address of a function, or is there another way to do this?

+4
source share
2 answers

This is not true. %papplies only to types of object pointers (in fact, void *). There is no format specifier for the function pointer.

+5
source

, printf.

 printf("The address of the function is =%p\n",test);

%p.

+2

All Articles