top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why there is no difference between funptr and &funptr ??

+4 votes
640 views
#include <stdio.h>
typedef void (*ptr)(void);
void fun (void)
{
    printf("\n In fun\n");
}
int main()
{
    ptr funptr1, funptr2, funptr3;
    funptr1 = fun;
    funptr2 = *fun;
    funptr3 = &fun;
    funptr1();
    funptr2();
    funptr3();
    return 0;
}  

====================================================
OUTPUT :=>

    $ ./a.exe

     In fun

     In fun

     In fun

The address stored in funptr1, funptr2 and funptr3 is same why ??

posted Oct 17, 2013 by Vikas Upadhyay

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Same is true with array also, I quickly tested with a[10], a and &a is same.
Both are same i.e. fun and &fun but the type is different fun is type void(void) while &fun is void (*)(void). Same is true with Array.

3 Answers

+2 votes
 
Best answer

For Function compiler when it is converting c code to assembly taking decision ...Internally all remains the same value for function pointer ....

For your code i generated assembly instruction for each thing you see it is same instruction ....It means that compiler only taking decision for this that behave &func, *func and func same....

see your code and compare with these instruction Both assigning and retrieving process is same

10:x.c           ****     funptr1 = fun;      // Assigning 1 
  50                    .loc 1 10 0
  51 001d C7442404      movl    $fun, 4(%esp)
  51      00000000
  11:x.c           ****     funptr2 = *fun;   //Assigning 2 
  52                    .loc 1 11 0
  53 0025 C7442408      movl    $fun, 8(%esp)
  53      00000000
  12:x.c           ****     funptr3 = &fun;   //Assigning 3 
  54                    .loc 1 12 0
  55 002d C744240C      movl    $fun, 12(%esp)
  55      00000000
  13:x.c           ****     funptr1();   //calling 1 
  56                    .loc 1 13 0
  57 0035 8B442404      movl    4(%esp), %eax
  58 0039 FFD0          call    *%eax
  14:x.c           ****     funptr2(); //calling 2
  59                    .loc 1 14 0
  60 003b 8B442408      movl    8(%esp), %eax
  61 003f FFD0          call    *%eax 
  15:x.c           ****     funptr3();    //calling 3 
  62                    .loc 1 15 0 
  63 0041 8B44240C      movl    12(%esp), %eax
  64 0045 FFD0          call    *%eax
answer Nov 19, 2013 by Sachidananda Sahu
+2 votes

I wrote a simple program to see how variable is treated internally -

main()
{
  int a=10;
  printf("%u\n", a);
  printf("%u\n", &a);
  printf("%u\n", &&a);
}

It gives the error in last statement -

Based on this my interpretation
With most variables variable_name has a meaning other than getting the address of that variable, so you need to use &variable to get the address (where after single reference it is not making sense).
The unary &, when applied to a function, yields a pointer to the function, just like it yields the address of an object when it is applied to an object. For pointers to ordinary functions, it is always redundant because of the implicit function-to-function-pointer conversion.
The unary *, when applied to a function pointer, yields the pointed-to function, just like it yields the pointed-to object when it is applied to an ordinary pointer to an object.

answer Oct 18, 2013 by Salil Agrawal
+1 vote

Why there is no difference between funptr and &funptr ??

First of all,

funptr1 = fun;
funptr3 = &fun;

Above 2 do the same work. The & is optional, because when you mention the name of a function but are not calling it, there's nothing else you could possibly be trying to do except generate a pointer to it.So most guys prefer to write " funptr1 = fun".

funptr2 = *fun;

This is dereference and getting the value-at-address stored

answer Oct 18, 2013 by Satyabrata Mahapatra
Similar Questions
+3 votes

A list contains a set of numbers, one number presents once and other numbers present even no. of times. Find out the number that occurs once in the list.

+1 vote

int arr[ ] = { 1, 2 };
p = arr; /* p is pointing to arr */

How pointer p will behave ?

...