top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why sizeof to function returns 1 in C?

0 votes
429 views

Used the sizeof of function, which gives 1; why?
I want to know the size of the entire function. How to achive it?

#include <stdio.h>
void (*p)(int); 
void test_func(int data)
{
  printf("%d\n",data);
}

main(void)
{
    p = test_func;
    (*p)(4);
    printf("%d",sizeof(test_func));
}
posted Jul 28, 2015 by Saif Khanam

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

sizeof operator does not evaluate its operand, it just yield the size of test_func(..) type, i.e. sizeof(void).

Referece C99 standard is 6.5.3.4.

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the 
parenthesized name of a type. The size is determined from the type of the operand. The result 
is an integer.  If the type of the operand is a variable length array type, the operand is evaluated; 
otherwise, the operand is not evaluated and the result is an integer constant.
answer Jul 28, 2015 by Salil Agrawal
Similar Questions
+1 vote

Can someone help me with the example when to use normal function calling and when pointer function calling. What was the reason for introducing pointer function calling in C?

...