2

My first doubt is:

Is the name of function a pointer variable and Which variable's address is the pointer holding ? (in this program which variable's address addition pointer holds).

AND if i write '&' before addition, program runs fine. Is compiler automatically add '&' during compilation ???

#include<stdio.h>
int addition(int, int);
int main() 
{
int (*p)(int, int);
int sum;
p=addition;  // if I add '&'  before function name the            
//program runs fine. (&addition).
sum=p(10, 20);
printf("Sum is %d\n", sum);
return 0;
} 
int addition(int x, int y)
{
int r;
r=x+y;
return r;
}

Output is: Sum is 30.

henrybbosa
  • 1,139
  • 13
  • 28
Aman Warudkar
  • 125
  • 1
  • 1
  • 12

4 Answers4

3

In C, using the name of a function in a value context implicitly gives the address of that function. There is nothing else that it could give, so this is a convenience. But using an explicit & in front also works, as you have discovered. The result is the same either way.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
2

int (*p)(int, int);

Here you are declaring a pointer to a function.

In p = addition; You are pointing p to addition function. Just as variables which are declared have addresses, functions too have their own addresses. So, by doing that you are assigning the address of addition to p and thus p points to addition function.

Hope this explanation clears your doubts.

Just like arrays, assigning a function name or address of the function to a pointer variable is the same. so your choice to place a & before addition.

J...S
  • 5,079
  • 1
  • 20
  • 35
VVV
  • 47
  • 6
1

Function names are pointer-like and function pointers are function-like:

#include <stdio.h>
void hw(void) { puts("hello world"); }
int main()
{
    /*all three work*/
    hw();
    (&hw)();
    (*&hw)();
}

That makes them a little bit like arrays in that they autoconvert to pointers and that you can't really get to the "value" of a function (a block of op codes), which is because C doesn't have any operations on it.

The difference between functions and function pointers is blurred in most contexts in C, but it does come up if you typedef.

#include <stdio.h>
typedef void void_void_t(void);      //a function typedef
typedef void (*void_void_pt)(void);  //a function pointer typedef
void_void_t hw;  //no storage, only verifies that hw matches the signature
void_void_pt hwp; //a null pointer of the void_void_pt type; takes up memory
void hw(void) { puts("hello world"); }
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
0

The name of a function is not a pointer. What you're seeing is a result of how expressions work in C.

However, when used in an expression like

 p = addition;

the name of the function is implicitly converted to a pointer. In this context, p = addition (which implicitly converts addition to a pointer, and assigns the result to p) and p = &addition (which computes a pointer with the same value, and assigns the result to p) have the same net effect.

Peter
  • 35,646
  • 4
  • 32
  • 74