Function always evaluating true?

This code is supposed to return the address of Dummy. However when I compiled it a messge saying the function will always return true showed up. I don't see what is wrong and would like a seccond set of eyes to look at the code.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

void Dummy();

int main ()
{
   Dummy();
   cout << "the address or Dummy() is: "
        << &Dummy
        << endl;
        
         
    system("PAUSE");
    return 0;
}

void Dummy()
{
     cout << "Dummy() has been executed."
          << endl;
}
I'm not sure if it's even legal to use the & on a function. I haven't seen that used on a function before, not even on pointers to funtions.

The compiler is probably interpreting it as something totally different like the bitwise AND operator (&) which is actually the same symbol as well. This would make it a condition, and if it's a condition it would either return true or false. That's pretty much my guess on the warnings you are getting.
Post the actual compile error. The line number would be helpful.
I have the same problem as Shinji. Maybe we're using the same book (I'm using "Learn to program with C++", written by John Smiley)?

The compiler doesn't give a compile-error, but a warning:
"[Warning] the address of 'void Dummy()', will always evaluate as 'true'"

This warning is referring to the line where "&Dummy" is written.

And indeed, when you run the program the number "1" (=true) is printed.

The problem gets worse when you actually assign the address-location to a pointer-variable (that you declare as integer).

1
2
int* pDummy=0;
pDummy=&Dummy;


Then you get this compile-error:
"cannot convert 'void(*)()' to 'int*' in assignement"

Of course, due to the fact that the address of the function will evaluate as true, this address cannot be stored in the integer pointer-variable (bool *pDummy works fine).

I just don't get it. The address is supposed to be hexadecimal. Or is this a fault of the writer John Smiley?


Last edited on
int* is not the right pointer type, the right declaration should be void (*pDummy) ();.
You can cast it like this
1
2
int* pDummy=0;
pDummy = (int*)Dummy; 
Pointers to functions are converted to numerical types always as 'true'. To actually get the value, first cast to void *. You can then cast to anything else you want, or just print the value.
I believe the original problem is that ostream does not have an overload for pointer types, so the compiler is performing an implicit boolean conversion and using operator<<( bool ). Hence the reason for outputting 1.

Hmm... C++ is rather schizophrenic in this whole area.

1
2
unsigned a,
    b=&a;
generates a compiler error, but
unsigned a=&main;
generates a compiler warning and a==1.

1
2
unsigned a,
    b=(unsigned)&a;
and
unsigned a=(unsigned)&main;
both work as expected, however.

I think the compiler should demand explicit conversions for all pointer types when assigning to numeric types.
Last edited on
Like JSmith said - the compiler is choosing what it believes to be the
closest fit.
Mingw uses bool, but MSVC uses *void - so MSVC will display the function address.
Topic archived. No new replies allowed.