Basic Function pointers

Nov 15, 2015 at 6:53pm
The tutorial I am reading, states (in a nutshell) that a function name acts as a pointer to the function. Here is the link.

http://www.learncpp.com/cpp-tutorial/78-function-pointers/

In the example below, I am expecting that the output statement will print out the address being held by the function pointer foo.

Instead, the compiler generates a warning as follows:

warning: the address of 'int foo()' will always evaluate as 'true' [-Waddress]|

and when I run the program the output I get is the number 1.

Can anyone explain what I am missing here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int foo()
{
    return 5;
}

int main()
{
    cout << foo << "\n";

    return 0;
}
Nov 15, 2015 at 7:03pm
You're missing the parenthesis after foo.

cout << foo() << "\n"; // add () after you call functions
Last edited on Nov 15, 2015 at 7:31pm
Nov 15, 2015 at 7:08pm
http://stackoverflow.com/questions/2064692/how-to-print-function-pointers-with-cout

You're missing the parenthesis after foo.

Did you read the actual question? The lack of parenthesis is deliberate.
Nov 15, 2015 at 7:11pm
closed account (E0p9LyTq)
You are not missing anything, it is your compiler that is the "problem."

I tried your code in Visual Studio 2015 (Community) and in TDM-GCC 4.9.2 (Orwell's Dev-C++).

VS prints out the function's address, as the tutorial expects.

TDM-GCC prints out the same as you receive, with the same warning.

Nov 15, 2015 at 7:24pm
I had a feeling that might be the reason. The author is using VS.

I am also using GCC from MinGW

Thanks for the quick reply.
Last edited on Nov 15, 2015 at 7:24pm
Topic archived. No new replies allowed.