回 帖 发 新 帖 刷新版面

主题:[转帖]用程序证明 NULL '\0' 0 的不同

编译以下程序,你将得到编译器的警告

/* test.c */

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

int main(void)
{
        int i = 5;
        int* ptr = malloc(1);
        char ch = 'e';
        /* Checking an int vs the other two */
        if ( i == NULL)
                puts ("This shouldn't happen");
        else
                puts ("But this should");
        if ( i == '\0')
                puts ("This shouldn't happen");
        else
                puts ("But this should");
        /* Checking a ptr vs the other two */
        if ( ptr == 0)
                puts ("This shouldn't happen");
        else
                puts ("But this should");
        if ( ptr == '\0')
                puts ("This shouldn't happen");
        else
                puts ("But this should");
        /* Checking a character vs the other two */
        if ( ch == NULL)
                puts ("This shouldn't happen");
        else
                puts ("But this should");
        if ( ch == 0)
                puts ("This shouldn't happen");
        else
                puts ("But this should");
        free(ptr);
        
        getchar();
        return 0;
}

==========================================
test.c: In function `main':
test.c:10: warning: comparison between pointer and integer
test.c:28: warning: comparison between pointer and integer

我用的是 Dev-C++
可以在这里下载 http://free3.e-168.cn/antigloss/blog.php?do_showone/tid_42.html

此外,大家也可以参考这个网站 http://www.eskimo.com/~scs/C-faq/s5.html

回复列表 (共23个回复)

沙发

--------------------Configuration: value - Win32 Debug--------------------
Compiling...
main.cpp
E:\6.0\C\value\main.cpp(9) : error C2440: 'initializing' : cannot convert from 'void *' to 'int *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Error executing cl.exe.

value.exe - 1 error(s), 0 warning(s)

//这是直接运行的结果 VC6.0
//修改后int* ptr = (int *)malloc(1);

--------------------Configuration: value - Win32 Debug--------------------
Compiling...
main.cpp
Linking...

value.exe - 0 error(s), 0 warning(s)

板凳

--------------------Configuration: value - Win32 Debug--------------------
Compiling...
main.cpp
E:\6.0\C\value\main.cpp(9) : error C2440: 'initializing' : cannot convert from 'void *' to 'int *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Error executing cl.exe.

value.exe - 1 error(s), 0 warning(s)

//这是直接运行的结果 VC6.0
//修改后int* ptr = (int *)malloc(1);

--------------------Configuration: value - Win32 Debug--------------------
Compiling...
main.cpp
Linking...

value.exe - 0 error(s), 0 warning(s)

3 楼

C++Builder6同样不报错
我刚刚测试了

4 楼

我们在讨论 C,请你改成 main.c 可以吗?

5 楼

不管编译器是什么结果,你去这里看看就知道它们的不同了。不过前提是要懂一点英文
http://www.eskimo.com/~scs/C-faq/s5.html

6 楼

值是相同的吧?只是类型不同了。
NULL是void *
'\0'是char
0是int

7 楼

不好意思,程序出错了,多发了一个。

8 楼

指针和数字有可比性吗?可以说指针的值和整数的值相等?哈哈!

9 楼

有什么不对的吗?NULL和0,它们的二进制代码一样。

10 楼

#include<stdio.h>
int main(void)
{    int i = 0;
    char *pc = NULL;
    if(i==pc)
        printf("yes\n");
    else
        printf("no\n");
    return 0;
}

在TC3里面保存为.c格式的文件,编译运行(有1个警告),输出yes
警告不过是说他们类型不同罢了,但二进制值的确是相等的。

我来回复

您尚未登录,请登录后再回复。点此登录或注册