This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

func and func()

Hi All,

let a function prototype is :

bit func();

Now if it is used as follows (inadvertently) then what would it do ?? Why not the complier throws an error ??

if (func) {
...
...
}
else {
...
...
}

-Rocknmoon()

  • There is nothing wrong with what you did. You are checking to see if func is a null pointer or not, it isn't. The compiler could warn that your if clause is always constant but maybe you don't have the warning level up. Or maybe you forgot to run you code through Lint, but who would do that, eh?

    By the way, excluding the (void) in your prototype means that the compiler will use the first occurance of the function's usage as the true prototype. This is not C++ so to be strictly proper you should define and prototype the function identically, e.g.

    bit func(void) { return bitVar }
    and
    bit func(void);

  • "if (func)"

    Funnily enough, I made that very same mistake only the other day!

    As Mark says, the expression 'func' has the value of the address of func; the if simply tests if the value is zero or not - almost undoubtedly it won't be, so it will always equate to "true"

    "The compiler could warn..."

    As I have noted several timed before, "small" cross-compilers like C51 are generally weaker in their diagnostic messages than their "big" native cousins like MSVC, Borland, etc
    Therefore it is often worthwhile running your code through a "big" compiler for the benefit of the more extensive diagnostics.
    Or use lint