Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do function pointers need an ampersand [duplicate]

In C/C++, if I have a the following functions:

void foo(); void bar(void (*funcPtr)()); 

Is there a difference between these two calls:

bar(foo); bar(&foo); 

?

like image 899
Baruch Avatar asked Jun 04 '13 11:06

Baruch


People also ask

What does * and & indicate in pointer?

The fundamental rules of pointer operators are: The * operator turns a value of type pointer to T into a variable of type T . The & operator turns a variable of type T into a value of type pointer to T .

What can not be done with function pointers?

What will we not do with function pointers? Explanation: As it is used to execute a block of code, So we will not allocate or deallocate memory.

Do function pointers need to be freed?

No. You must not because free(ptr) is used only when pointer ptr is previously returned by any of malloc family functions. Passing free a pointer to any other object (like a variable or array element) causes undefined behaviour.

What does an ampersand do in C?

The ampersand is the address of operator. It returns the memory location of a variable and that's the only way it's used, prefixed to a variable like the engine on a train. The variable doesn't even have to be initialized, just declared.


1 Answers

No, there is no difference, since function can be implicitly converted to pointer to function. Relevant quote from standard (N3376 4.3/1).

An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function.

like image 163
ForEveR Avatar answered Oct 07 '22 20:10

ForEveR