关于NULL的问题 [英] A question about NULL

查看:97
本文介绍了关于NULL的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是C ++的新手(在今年夏天开始学习),我

有以下问题(对不起,如果它听起来愚蠢):

在许多代码示例和源文件中,我看到正在使用NULL表达式(对于

示例,int * someInt = NULL;)。我自己使用了类似的初始化,即使我没有定义NULL,它也可以正常工作。但是什么是NULL究竟?它是由编译器定义的

常量吗?以下

两种初始化指针的方法有什么区别吗?

//示例1

int * myInt = NULL;


//示例2

int * myInt = 0;


如果没有差异,我为什么要使用NULL而不是0?

提前致谢。


Dmitry

PS这是我在新闻组中的第一篇文章:)

Hi,
I''m new to C++ (started learning in the beginning of this summer), and I
have the following question (sorry if it sounds stupid):
In many code samples and source files, I see NULL expression being used (for
example, int* someInt=NULL; ). I used similar initialization myself, and it
works fine even if I don''t define NULL. But what is "NULL" exactly? Is it a
constant defined by compiler? Is there any difference between the following
two ways to initialize a pointer?
// example 1
int * myInt = NULL;

// example 2
int * myInt = 0;

If there is no difference, why would I use NULL instead of 0?
Thanks in advance.

Dmitry
P.S. This is my first post in the newsgroup :)

推荐答案



" Dmitry D" < WO **** @ telus.net>在消息中写道

news:xv ********************* @ news0.telusplanet.net ...

"Dmitry D" <wo****@telus.net> wrote in message
news:xv*********************@news0.telusplanet.net ...
我是C ++的新手(在今年夏天开始学习),我有以下问题(对不起,如果听起来很愚蠢):
很多代码示例和源文件,我看到使用
的NULL表达式(例如,int * someInt = NULL;)。我自己使用了类似的初始化,并且
它工作正常,即使我没有定义NULL。但是什么是NULL究竟?它是
编译器定义的常量吗?两种初始化指针的方法之间的
有什么区别吗?
//示例1
int * myInt = NULL;

//示例2
int * myInt = 0;
Hi,
I''m new to C++ (started learning in the beginning of this summer), and I
have the following question (sorry if it sounds stupid):
In many code samples and source files, I see NULL expression being used (for example, int* someInt=NULL; ). I used similar initialization myself, and it works fine even if I don''t define NULL. But what is "NULL" exactly? Is it a constant defined by compiler? Is there any difference between the following two ways to initialize a pointer?
// example 1
int * myInt = NULL;

// example 2
int * myInt = 0;




在C中,NULL的定义如下:


# define NULL(void *)0

在C ++中,NULL定义如下:


#define NULL 0

如果你尝试初始化一个指向void的指针类型的指针,你会用C ++编译器得到错误:


int * p =(无效*)0; //确定''C *,C ++中的错误

int * p = 0; //在C ++中运行,'C'中的错误'

C ++专门禁止NULL的''C''样式定义。为什么?也许

为''C''样式分配一个''类型'指针'NULL可以被认为是

类似于指定派生类型的''指针''到'''指向基地的指针

类型'',这是错误的。然后,再次指定一个''指向一个类型的指针''

到零值是更正确的;除此之外''void''不是所有类型的基础

类型?你可以说它有点混乱(我这样做)。然后再次获得
,C ++并不是真正的OO语言;这是'C'的OO扩展的包袱。

本质上只是便携式汇编程序。为了正确地完成工作,你需要一种语言,它具有适用于所有类型的通用基本类型,并且每种派生类型都需要Null

实例。



In ''C'', NULL is defined like this:

#define NULL (void*)0
In C++, NULL is defined like this:

#define NULL 0
If you try and initialise a pointer to a type with a pointer to void, you''ll
get errors with a C++ compiler:

int* p = (void*)0; // OK in ''C*, error in C++
int* p = 0; // OK in C++, error in ''C''
C++ specifically forbids the ''C'' style definition of NULL. Why? Perhaps
assigning a ''pointer to a type'' to the ''C'' style NULL could be considered
similar to assigning a ''pointer to a derived type'' to a ''pointer to base
type'', which would be wrong. Then again, is assigning a ''pointer to a type''
to the value zero is any more correct; besides which ''void'' is not the base
type of all types? You could say that it''s a bit of shambles (I do). Then
again, C++ isn''t a real OO language; it''s a baggage of OO extensions to ''C''
which is, essentially, just portable assembler. To do the job properly, you
would need a language that had a common base type for all types and Null
instances of every derived type.


Dmitry D写道:
Dmitry D wrote:

我是C ++的新手(在今年夏天开始学习),我是
有以下问题(对不起,如果它听起来很愚蠢):
在许多代码示例和源文件中,我看到正在使用NULL表达式(例如,对于
示例,int * someInt = NULL;)。我自己使用了类似的初始化,即使我没有定义NULL,它也能正常工作。但是什么是NULL究竟?它是由编译器定义的常量吗?以下两种初始化指针的方法有什么区别吗?
//示例1
int * myInt = NULL;

//示例2
int * myInt = 0;

如果没有区别,为什么我会使用NULL而不是0?
在此先感谢。

Dmitry
Hi,
I''m new to C++ (started learning in the beginning of this summer), and I
have the following question (sorry if it sounds stupid):
In many code samples and source files, I see NULL expression being used (for
example, int* someInt=NULL; ). I used similar initialization myself, and it
works fine even if I don''t define NULL. But what is "NULL" exactly? Is it a
constant defined by compiler? Is there any difference between the following
two ways to initialize a pointer?
// example 1
int * myInt = NULL;

// example 2
int * myInt = 0;

If there is no difference, why would I use NULL instead of 0?
Thanks in advance.

Dmitry
P.S. This is my first post in the newsgroup :)




NULL通常是一个宏


#define NULL 0

有时(但仅限于C作为C ++


#define NULL((void *)0)

'' 0''是一个特殊情况,意思是


int * ptr = 0; //开心的编译器


完全合法但是


int * ptr = 1; //非法 - 胡思乱想的编译器


错误:从`int''无效转换为`int *''

为什么是NULL?有些人认为可读性。有人会认为NULL是

而不是'0',例如我*可以*(但我永远不会定义

NULL和((void *)1)。


NULL通常在其中一个标准头文件中定义。



NULL is usually a macro

#define NULL 0

and sometimes (but only for C as C++

#define NULL ((void *) 0)
''0'' is a special case in the sense that

int * ptr = 0; // happy compiler

Is perfectly legal ... but

int * ptr = 1; // Illegal - cranky compiler

error: invalid conversion from `int'' to `int*''
Why NULL? Some would argue readability. Some would argue that NULL is
not neccassarily ''0'', for example I *could* (but I would never) define
NULL and ((void *) 1).

NULL is usually defined in one of the standard header files.


Dmitry D在新闻中写道:xv ********************* @ news0.telusplanet.net:
Dmitry D wrote in news:xv*********************@news0.telusplanet.net :

我是C ++的新手(开始学习ning在今年夏天开始),并且我有以下问题(对不起,如果它听起来很愚蠢):
在许多代码示例和源文件中,我看到使用了NULL表达式(例如,int * someInt = NULL; )。我自己使用了类似的初始化,即使我没有定义NULL也能正常工作。但是什么是
NULL究竟?它是由编译器定义的常量吗?以下两种初始化指针的方法之间是否有任何区别?
//示例1
int * myInt = NULL;

//示例2
int * myInt = 0;

如果没有区别,为什么我会使用NULL而不是0?
提前致谢。
Hi,
I''m new to C++ (started learning in the beginning of this summer), and
I have the following question (sorry if it sounds stupid):
In many code samples and source files, I see NULL expression being
used (for example, int* someInt=NULL; ). I used similar initialization
myself, and it works fine even if I don''t define NULL. But what is
"NULL" exactly? Is it a constant defined by compiler? Is there any
difference between the following two ways to initialize a pointer?
// example 1
int * myInt = NULL;

// example 2
int * myInt = 0;

If there is no difference, why would I use NULL instead of 0?
Thanks in advance.



NULL是#define'的宏使用它你必须包含

中的一个定义它的系统头,但不要使用它。 br />

在C ++中,NULL通常定义为0或0L,因此在您给出的2个示例中没有

的差异。

在C中,NULL通常被定义为((void *)0),这是更多

typesafe"在C.


因此,如果你有一些C编译器和C ++编译器可以看到的代码,那么宏NULL很有用,否则使用0表示C ++。


Rob。

-
http://www.victim-prime.dsl.pipex.com/



NULL is a #define''s macro to use it you have to include one of
the system header''s that defines it, but don''t use it.

In C++ NULL is usually defined to be 0 or 0L, so there is no
difference in the 2 examples you gave.

In C NULL is usually defined to be ((void *)0), this is more
"typesafe" in C.

So the macro NULL is useful if you have some code that is going to
bee seen by a C compiler and a C++ compiler, otherwise use 0 for C++.

Rob.
--
http://www.victim-prime.dsl.pipex.com/


这篇关于关于NULL的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆