-3

I'm using the XC8 compiler which targets 8 bit microcontrollers.

This does not produce any warnings or errors, but hangs the microcontroller anyway:

    uint8_t some_array[4];
    uint8_t        // no compile errors at all
    some_function();

Another thing I've noticed, expect this one does not crash the microcontroller, and seems to return 0:

printf("%c", some_function); 

In this one, I'm calling a function which never returns a 0. I've forgotten to add the () but it compiles and somehow runs anyway, but with a wrong return value.

Joe
  • 41,484
  • 20
  • 104
  • 125
user9993
  • 5,833
  • 11
  • 56
  • 117
  • Leaving out the `()` means you return the location of the function as a value, so that part compiling is 100% normal. – Charles Duffy Apr 19 '15 at 16:22
  • BTW, since you have two separate questions, you should be asking them as... two separate questions. Munging two questions into one makes this too broad. – Charles Duffy Apr 19 '15 at 16:22
  • I see. Isn't & used to get the address? – user9993 Apr 19 '15 at 16:23
  • 1
    The first code is valid. What thing should generate warning or error? – j123b567 Apr 19 '15 at 16:32
  • The second code is also valid, it prints the lower byte of the function's address. Oh and the '&' is implicit for functions and array (although adding it doesn't hurt), it's only mandatory for variables as you can pass them either by address or value. Functions and arrays can only be passed by address, so the '&' is not mandatory. – Bregalad Apr 19 '15 at 16:35

2 Answers2

1

See here: Why is using the function name as a function pointer equivalent to applying the address-of operator to the function name?

The name of a function is just the address of it.(In this point, C and C++ are same). just like the name of an array is its starting address.

& is used to get address for an variable, but the function name is already the address. So we dont need '&' here.

Community
  • 1
  • 1
Andrinux
  • 55
  • 7
0

Non-printable characters (e.g. spaces, newlines and tabs) are ignored by the compiler, so the first example does not produce compilation errors and though does not produce runtime errors, so the reason of crash is something else.

In the second example, braces absence must produce compilation error. If it doesn't, then it's a compiler issue.

SNC
  • 23
  • 3
  • 2
    It should just produce some warning that you are trying to print character, but you are actually printing the pointer. Using function name without braces is valid, it is just pointer to the function. – j123b567 Apr 19 '15 at 16:35