回 帖 发 新 帖 刷新版面

主题:[转帖]用程序证明 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个回复)

11 楼

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

12 楼

在TC3里面保存为.c格式的文件,编译运行(有1个警告),输出yes
警告不过是说他们类型不同罢了,但二进制值的确是相等的。
====================
懒得再说了~既然你们一定要这么认为~我也无所谓~

13 楼

你把事情的因果关系搞清了,再来发贴.
你的例子只能证明他们一样.
因为他们一样 而在c下却出现了警告,所以才出现在头文件中的分情况预定义
不同的东西,定义相同也是不同
相同的东西,定义不同也是相同

14 楼

对不起,看来是我太想当然了。

到楼主给的连接去看了一下:
Question 5.2
How do I get a null pointer in my programs?

According to the language definition, a constant 0 in a pointer context is converted into a null pointer at compile time. That is, in an initialization, assignment, or comparison when one side is a variable or expression of pointer type, the compiler can tell that a constant 0 on the other side requests a null pointer, and generate the correctly-typed null pointer value. Therefore, the following fragments are perfectly legal:

    char *p = 0;
    if(p != 0)
在编译的时候,在涉及0与指针的运算中,把0转换为NULL,是这样吧?也就是说NULL的二进制值也不一定就是0。
这段话后面还说了在函数调用的时候,如果参数应该是一个指针,而调用时传递的是0,就可能出现问题。

另外我还看见了一点:
As mentioned above, there is a null pointer for each pointer type, and the internal values of null pointers for different types may be different. Although programmers need not know the internal values, the compiler must always be informed which type of null pointer is required, so that it can make the distinction if necessary

就是说不同类型的NULL指针也是不同的?前些时候我一直以为NULL就是void *类型的……看来又错了。

15 楼

Seriously, have any actual machines really used nonzero null pointers, or different representations for pointers to different types?


--------------------------------------------------------------------------------

The Prime 50 series used segment 07777, offset 0 for the null pointer, at least for PL/I. Later models used segment 0, offset 0 for null pointers in C, necessitating new instructions such as TCNP (Test C Null Pointer), evidently as a sop to all the extant poorly-written C code which made incorrect assumptions. Older, word-addressed Prime machines were also notorious for requiring larger byte pointers (char *'s) than word pointers (int *'s).

http://www.eskimo.com/~scs/C-faq/q5.17.html

请你们认真看看这里 http://www.eskimo.com/~scs/C-faq/s5.html

16 楼

就是说不同类型的NULL指针也是不同的?前些时候我一直以为NULL就是void *类型的……看来又错了。
===================
C 语言中,void 类型在赋值的时候会自动转换成相应的类型

17 楼

还存在个NUL
你也拿来比一下

18 楼

C 语言没有定义 NUL

19 楼

感谢楼主。
看来还得多认真学习。

20 楼

NULL
Remarks

NULL is the null-pointer value used with many pointer operations and functions.


#include <iostream.h>
#include <typeinfo.h>
void main()
{
    const type_info& p=typeid(NULL);
    cout<<p.name()<<endl;
}
结果:
int
Press any key to continue

结果显示NULL是int 型??!!

我来回复

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