Why does a “function name” evaluate to true in C and how to get warned on it

前端 未结 5 2138
粉色の甜心
粉色の甜心 2020-12-31 11:14

I recently stumbled across the following behaviour of gcc 3.2.2 writing a c program:

In an if statement I forgot the braces of a function and wrote:

if

5条回答
  •  感情败类
    2020-12-31 11:58

    myFunc is simply the memory address of the function, and is non-zero.

    Your if-statement is pretty much the same as writing:

    if (0x08451234) { ... }
    

    And as a non-zero value, it is true.

    No warning seems appropriate, as it is valid and even somewhat common to test function-pointers to see if they are NULL or not.

提交回复
热议问题