Sample code:
int f1(char c){ return c; };
int f2(int i ){ return i; };
int main(void)
{
return (1 ? f1 : f2)(0);
}
Different behavior between compilers:
gcc (latest): gcc prog.c -Wall -Wextra -std=c89 -pedantic
prog.c: In function 'main':
prog.c:6:18: warning: pointer type mismatch in conditional expression
6 | return (1 ? f1 : f2)(0);
| ^
prog.c:6:18: error: called object is not a function or function pointer
6 | return (1 ? f1 : f2)(0);
| ~~~~~~~~^~~~~
prog.c:7:1: warning: control reaches end of non-void function [-Wreturn-type]
7 | }
| ^
clang (latest): clang prog.c -Wall -Wextra -std=c89 -pedantic
prog.c:6:13: warning: pointer type mismatch ('int (*)(char)' and 'int (*)(int)') [-Wpointer-type-mismatch]
return (1 ? f1 : f2)(0);
^ ~~ ~~
prog.c:6:23: error: called object type 'void *' is not a function or function pointer
return (1 ? f1 : f2)(0);
~~~~~~~~~~~~~^
1 warning and 1 error generated.
cl (latest): cl t1.c
t1.c(6): warning C4028: formal parameter 1 different from declaration
Microsoft (R) Incremental Linker Version 14.25.28611.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:t1.exe
t1.obj
Questions:
- What the C standard says?
- gcc & clang: why
called object is not a function or function pointer
? Then what is it? - cl gives the warning, but compiles the code. Does this behavior conform to the C standard?
- Why clang
called object type 'void *'
, but gcccalled object
? Where thisvoid *
comes from?
..." Your code doesn't satisfy any on the constraints in the list. So your code is invalid.
– Support Ukraine Oct 25 '20 at 19:47