如果我写
int zero = 0;
void *p1 = (void *)0;
void *p2 = (void *)(int)0;
void *p3 = (void *)(0 /*no-op, but does it affect the next zero?*/, 0);
void *p4 = (void *)zero; // For reference, this is a pointer to address zero
void *p5 = 0; // For reference, this is a null pointer
void *p6 = NULL; // For reference, this is a null pointer
void *p7 = nullptr; // For reference, this is a null pointer …Run Code Online (Sandbox Code Playgroud) 我知道这段代码不能用作"预期".只是快速查看此代码,我们认为返回值应为1,但在执行时它返回3.
// incorrect
variable = 1;
switch (variable)
{
case 1, 2:
return 1;
case 3, 4:
return 2;
default:
return 3;
}
Run Code Online (Sandbox Code Playgroud)
并且有一些正确的选项可以做到这一点:
// correct 1
variable = 1;
switch (variable)
{
case 1: case 2:
return 1;
case 3: case 4:
return 2;
default:
return 3;
}
Run Code Online (Sandbox Code Playgroud)
要么
// correct 2
switch (variable)
{
case 1:
case 2:
return 1;
case 3:
case 4:
return 2;
default:
return 3;
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么不正确的表单编译没有错误甚至警告(至少在Borland C++编译器中).
编译器在该代码中理解什么?