By WiSaGaN

2012-08-28 02:50:12 8 Comments

It's interesting that using the function name as a function pointer is equivalent to applying the address-of operator to the function name!

Here's the example.

typedef bool (*FunType)(int);
bool f(int);
int main() {
  FunType a = f;
  FunType b = &a; // Sure, here's an error.
  FunType c = &f; // This is not an error, though. 
                  // It's equivalent to the statement without "&".
                  // So we have c equals a.
  return 0;

Using the name is something we already know in array. But you can't write something like

int a[2];
int * b = &a; // Error!

It seems not consistent with other parts of the language. What's the rationale of this design?

This question explains the semantics of such behavior and why it works. But I'm interested in why the language was designed this way.

What's more interesting is the function type can be implicitly converted to pointer to itself when using as a parameter, but will not be converted to a pointer to itself when using as a return type!


typedef bool FunctionType(int);
void g(FunctionType); // Implicitly converted to void g(FunctionType *).
FunctionType h(); // Error!
FunctionType * j(); // Return a function pointer to a function 
                    // that has the type of bool(int).


@Jesse Good 2012-08-28 04:17:29

For arrays, there is no pointer decay when the address-of operator is used:

int a[2];
int * p1 = a;      // No address-of operator, so type is int*
int (*p2)[2] = &a; // Address-of operator used, so type is int (*)[2]

This makes sense because arrays and pointers are different types, and it is possible for example to return references to arrays or pass references to arrays in functions.

However, with functions, what other type could be possible?

void foo(){}
&foo; // #1
foo;  // #2

Let's imagine that only #2 gives the type void(*)(), what would the type of &foo be? There is no other possibility.

@Herdsman 2020-01-10 21:36:12

Can you please explain type (*)[2]. I do not know if I can imagine it correctly, but is it like third value of array (pointer) p2? And it's value is also address? As (*)[2] = &. Correct my notion of value of pointer (*)[2], but it basically is, isn't?

@Jesse Good 2020-01-11 01:04:43

@Herdsman: int (*)[2] is a pointer to an array of 2 ints. This site gives a detailed explanation.

@Herdsman 2020-01-11 10:43:54

second or third? You didn't count from zero index

@Jesse Good 2020-01-11 10:57:13

Did you read my link? int (*)[2] is not indexing into an array. It says that the array has a size of 2.

@Michael Burr 2012-08-28 03:43:42

Since you specifically ask for the rationale of this behavior, here's the closest thing I can find (from the ANSI C90 Rationale document - Function calls

Pointers to functions may be used either as (*pf)() or as pf(). The latter construct, not sanctioned in the Base Document, appears in some present versions of C, is unambiguous, invalidates no old code, and can be an important shorthand. The shorthand is useful for packages that present only one external name, which designates a structure full of pointers to object s and functions : member functions can be called as instead of (* The treatment of function designators can lead to some curious , but valid , syntactic forms . Given the declarations :

int f ( ) , ( *pf ) ( ) ; 

then all of the following expressions are valid function calls :

( &f)(); f(); (*f)(); (**f)(); (***f)();
pf(); (*pf)(); (**pf)(); (***pf)();

The first expression on each line was discussed in the previous paragraph . The second is conventional usage . All subsequent expressions take advantage of the implicit conversion of a function designator to a pointer value , in nearly all expression contexts . The Committee saw no real harm in allowing these forms ; outlawing forms like (*f)(), while still permitting *a (for int a[]), simply seemed more trouble than it was worth .

Basically, the equivalence between function designators and function pointers was added to make using function pointers a little more convenient.

@Toby 2016-09-21 10:45:16

I imagine this was because function designators are really syntactic sugar for human purposes. A function name is really just a label on a memory address. The environment doesn't actually see separate functions and pointers to them - it just sees an instruction branch to an address. This is different to objects for which an environment will use different addressing modes (direct or indirect)

@Jerry Coffin 2012-08-28 02:57:53

It's a feature inherited from C.

In C, it's allowed primarily because there's not much else the name of a function, by itself, could mean. All you can do with an actual function is call it. If you're not calling it, the only thing you can do is take the address. Since there's no ambiguity, any time a function name isn't followed by a ( to signify a call to the function, the name evaluates to the address of the function.

That actually is somewhat similar to one other part of the language -- the name of an array evaluates to the address of the first element of the array except in some fairly limited circumstances (being used as the operand of & or sizeof).

Since C allowed it, C++ does as well, mostly because the same remains true: the only things you can do with a function are call it or take its address, so if the name isn't followed by a ( to signify a function call, then the name evaluates to the address with no ambiguity.

@Cubbi 2012-08-28 03:44:12

The only things you can do with a function are call it or take its address -- you could also use it to initialize a reference to function.

@jamesdlin 2016-09-21 12:01:35

@Cubbi: C doesn't have references, and the whole point of this answer is that the behavior is inherited from C.

@zwol 2016-09-21 12:52:16

Technically, in C, a function name evaluates to the address of the function even when it is used in a function-call expression; the "expression that denotes the called function" is mandated to have pointer-to-function type, and to accommodate this, a bare function name evaluates as a pointer to the function "except when it is the operand of sizeof or unary &" (C99 §§ and respectively).

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] Why is 'this' a pointer and not a reference?

26 Answered Questions

[SOLVED] Why do we need virtual functions in C++?

7 Answered Questions

[SOLVED] How to print function pointers with cout?

  • 2010-01-14 14:22:26
  • ibread
  • 44931 View
  • 50 Score
  • 7 Answer
  • Tags:   c++

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

11 Answered Questions

[SOLVED] How do function pointers in C work?

  • 2009-05-08 15:49:17
  • Yuval Adam
  • 802893 View
  • 1244 Score
  • 11 Answer
  • Tags:   c function-pointers

1 Answered Questions

[SOLVED] Error with function pointer returning a (void *)

  • 2014-11-01 21:07:35
  • Jesse Ellison
  • 920 View
  • 1 Score
  • 1 Answer
  • Tags:   c function-pointers

3 Answered Questions

[SOLVED] typedef function pointers and extern keyword

1 Answered Questions

[SOLVED] Typedefs for function pointer type

Sponsored Content