| Some functions with no arguments (like 'off') don't work for me!

Previous | Miscellaneous | Next

Q: | When I try to turn the calculator off using off function from system.h, nothing happens. What is wrong?
A: | Most probably, you wrote
    off;
instead of
    off ();
This is very common misinterpretation of usage of argument-less function. You may ask why the compiler did not display any errors? See, whenever you use the function name alone (without the parenthesis), it is automatically converted to a pointer to the function (this is very useful feature if used properly). So, `'off'` alone is interpreted as a pointer, so you have an statement which consists of an effect-less expression, similarly as if you wrote
    2 + 3;
This is not illegal in C, but it has no any effect (eventually, you will be warned by the compiler that the statement has no effect, if you enable enough strong warning checking). Such misinterpretations are very often with newbie users, so I will elaborate this in more details. Suppose that you wrote
    key = ngetchx;
You probably want to call ngetchx function to get a keypress, but instead, you will assign the address of this function to the variable `'key'`, because the function name without parenthesis is automatically converted to a pointer (however, you will be warned by the compiler that it is very ugly to assign a pointer to an integer variable without an explicite typecast). Instead, you need to write
    key = ngetchx ();
Sometimes, you will not get even a warning. Look the following example:
    void *qptr = kbd_queue;
The intention was probably to put the address of the keyboard queue into the `'qptr'` variable, but instead, the address of the function kbd_queue itself will be taken! As the function name is converted to a pointer to a function, and as you assign it to a void pointer (which is assign-compatible with all other pointer types), you will not get even a warning!