简体   繁体   English

为什么我在声明中说int * p = NULL,但在测试中p!= NULL,为什么不用* p!= NULL来匹配声明?

[英]Why do I say int *p = NULL in the declaration, but p != NULL in the test, why not *p != NULL to match the declaration?

in c++, if we assign a pointer value to NULL ,why dont we check if *p!=NULL and instead p!=NULL ? 在c ++中,如果我们将指针值赋给NULL ,为什么我们检查*p!=NULL而不是p!=NULL

I found this code in a tutorial. 我在教程中找到了这段代码。

int *p = NULL;
char *q = NULL;
// ...
if (p!=NULL) cout << *p;

Thanks in advance 提前致谢

The * is doing two different things. *正在做两件事。 When you declare the variable, it means the variable is a pointer. 声明变量时,表示变量是指针。 When you use the variable, it means "dereference", that is take the value at the location the pointer is pointing to. 当您使用变量时,它意味着“取消引用”,即取指针所指向的位置的值。 Two totally different meanings. 两个完全不同的含义。

因为p是指针,而*p是它指向的对象。

I think you are confused. 我觉得你很困惑。 The code from the tutorial is creating a pointer and initializing it to NULL , then later it is testing if it is NULL . 本教程中的代码是创建一个指针并将其初始化为NULL ,然后它将测试它是否为NULL The reason why you don't check if *p != NULL is because that would be testing if what it points to is NULL , and not test if the pointer itself is NULL . 你不检查*p != NULL的原因是因为它会测试它指向的是NULL ,而不测试指针本身是否为NULL

Of course you can choose to test *p against any value you like provided it is a valid pointer... It all depends on what you want to do. 当然,如果它是一个有效的指针,你可以选择对任何你喜欢的值进行测试*p这一切都取决于你想做什么。

EDIT: 编辑:

You didn't assign NULL to *p , you assigned it to p . 您没有为*p分配NULL ,而是将其分配给p The statement int *p = NULL; 语句int *p = NULL; is the same as writing: 与写作相同:

int *p;
p = NULL;

int * is the type. int *是类型。

Basically, when you write: if(p != NULL) you are simply testing if p points to a place such that it is safe to use *p . 基本上,当你写: if(p != NULL)你只是测试p指向一个地方,以便使用*p是安全的。

The code is declaring a int pointer through the statement int *p = NULL . 代码通过语句int *p = NULL声明一个int指针。 Please note that these statements are equivalent: 请注意,这些陈述是等效的:

int *p = NULL;
int* p = NULL;

And both of these mean that p is a pointer to an integer which further means that p holds the address of an integer. 并且这两个都意味着p是指向整数的指针,这进一步意味着p保存整数的地址。 So, when the code later checks for 因此,当代码稍后检查时

if(p != NULL)

it is basically checking that whether the address contained in this pointer is NULL or not. 它基本上是检查该指针中包含的地址是否为NULL。 I hope it makes things clear. 我希望它能说清楚。

Because p is a pointer; 因为p是一个指针; it is a variable that holds a memory address of some object. 它是一个包含某个对象的内存地址的变量。

The code checks if the pointer is pointing to an object and, if it is, shows the value of that object on the screen. 代码检查指针是否指向对象,如果是,则在屏幕上显示该对象的值。


if (p != NULL) // "if p is pointing to an object then ..."
{
    cout << *p; // "show me the value of that object on the screen"
}
  • p is the address of the object p是对象的地址
  • *p is the value of the object, called dereferencing *p是对象的值,称为解除引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM