Printing Function Address

15,471

Solution 1

This is not correct. %p is only for object pointer types (in fact, void * specifically). There is no format specifier for a function pointer.

Solution 2

Without using the separate pointer for print the address of the function, You can use the name of the function in the printf.

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

For printing the address in the hexa-decimal format you can use the %p.

Share:
15,471
Admin
Author by

Admin

Updated on June 24, 2022

Comments

  • Admin
    Admin almost 2 years

    I've been trying a way to figure out how to print the address of the function, This 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;
    }
    

    It o/p something like this without any warning and errors

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

    My question whether using %p format specifier is the correct way to print the address of the function or is there any other way to do so?