C MSVS 2017:指针空检查未按应有的方式运行

C MSVS 2017:指针空检查未按应有的方式运行,c,pointers,null,visual-studio-2017,C,Pointers,Null,Visual Studio 2017,我编写了以下“Hello world!”级别的代码: #include <stdio.h> #include <stdint.h> #include <stdlib.h> int main(const int argc, const char *argv[]) { double *Bptr = NULL; printf("Bptr's current value is: %p\n", Bptr); double Barray = 1.

我编写了以下“Hello world!”级别的代码:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main(const int argc, const char *argv[]) {
    double *Bptr = NULL;
    printf("Bptr's current value is: %p\n", Bptr);
    double Barray = 1.02;
    Bptr = &Barray;
    if (Bptr == NULL); {printf("Bptr is still a null pointer: %p\n", Bptr); };
    if (!Bptr); {printf("Bptr is still a null pointer (check 2): %p\n", Bptr); };
    printf("The address of Bptr is: %p\n", Bptr);
    return 0;
}

毫不奇怪,这不是我的本意。这是我第一次遇到这个问题。这也是我第一次以“空项目”的形式启动MSVS项目,而不是Windows控制台/桌面应用程序,因此我怀疑这可能与此有关。有人对导致这种行为的原因有什么建议吗?

如果if语句后面有一个分号,请删除它,它就会起作用。:)

如果if语句后面有一个分号,请删除它,它就会工作。:)

if语句之后使用分号。这就是为什么不管条件如何,printf语句总是被执行的原因。不需要在大括号后加分号,它什么也不做

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main(const int argc, const char *argv[]) {
    double *Bptr = NULL;
    printf("Bptr's current value is: %p\n", Bptr);
    double Barray = 1.02;
    Bptr = &Barray;
    if (Bptr == NULL) {printf("Bptr is still a null pointer: %p\n", Bptr); }
    if (!Bptr) {printf("Bptr is still a null pointer (check 2): %p\n", Bptr); }
    printf("The address of Bptr is: %p\n", Bptr);
    return 0;
}
#包括
#包括
#包括
int main(常量int argc,常量char*argv[]){
double*Bptr=NULL;
printf(“Bptr的当前值为:%p\n”,Bptr);
双坝=1.02;
Bptr=&Barray;
如果(Bptr==NULL){printf(“Bptr仍然是NULL指针:%p\n”,Bptr);}
如果(!Bptr){printf(“Bptr仍然是空指针(检查2):%p\n”,Bptr);}
printf(“Bptr的地址是:%p\n”,Bptr);
返回0;
}

您在if语句之后使用了分号。这就是为什么不管条件如何,printf语句总是被执行的原因。不需要在大括号后加分号,它什么也不做

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main(const int argc, const char *argv[]) {
    double *Bptr = NULL;
    printf("Bptr's current value is: %p\n", Bptr);
    double Barray = 1.02;
    Bptr = &Barray;
    if (Bptr == NULL) {printf("Bptr is still a null pointer: %p\n", Bptr); }
    if (!Bptr) {printf("Bptr is still a null pointer (check 2): %p\n", Bptr); }
    printf("The address of Bptr is: %p\n", Bptr);
    return 0;
}
#包括
#包括
#包括
int main(常量int argc,常量char*argv[]){
double*Bptr=NULL;
printf(“Bptr的当前值为:%p\n”,Bptr);
双坝=1.02;
Bptr=&Barray;
如果(Bptr==NULL){printf(“Bptr仍然是NULL指针:%p\n”,Bptr);}
如果(!Bptr){printf(“Bptr仍然是空指针(检查2):%p\n”,Bptr);}
printf(“Bptr的地址是:%p\n”,Bptr);
返回0;
}

最后一行输出来自哪里?if条件后面没有分号。同样
if(Bptr==NULL)是条件后的空语句。
printf
是unconditional@kmchmk:此输出中没有任何代码-“输出”中的文本与代码中的文本不同。看起来它是手动输入的,而不是直接复制粘贴的-或者是从不同的代码输出的。然而,这是一个学术问题,因为问题只是一个空语句和无条件块的放置。什么是
B
?您的代码中没有
B
。请发布您正在编译的实际代码。最后一行输出来自哪里?if条件后面没有分号。同样
if(Bptr==NULL)是条件后的空语句。
printf
是unconditional@kmchmk:此输出中没有任何代码-“输出”中的文本与代码中的文本不同。看起来它是手动输入的,而不是直接复制粘贴的-或者是从不同的代码输出的。然而,这是一个学术问题,因为问题只是一个空语句和无条件块的放置。什么是
B
?您的代码中没有
B
。请发布您正在编译的实际代码。这当然解决了问题。我不敢相信我错过了这么愚蠢的事情。事实上,在这两个if声明之后,谢谢你。@FlorisSA;也许你不明智的代码布局让这些事情更难发现。总是把条件块大括号放在新行上会使分号更容易辨认。Clifford,我也这么想。谢谢你的建议!这当然解决了问题。我不敢相信我错过了这么愚蠢的事情。事实上,在这两个if声明之后,谢谢你。@FlorisSA;也许你不明智的代码布局让这些事情更难发现。总是把条件块大括号放在新行上会使分号更容易辨认。Clifford,我也这么想。谢谢你的建议!