这些空指针,或者是他们的指针到地址0? [英] Are these null pointers, or are they pointers to address 0?

查看:92
本文介绍了这些空指针,或者是他们的指针到地址0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我写

int zero = 0;

void *p1 = (void *)0;
void *p2 = (void *)(int)0;
void *p3 = (void *)(0 /*no-op, but does it affect the next zero?*/, 0);

void *p4 = (void *)zero;    // For reference, this is a pointer to address zero
void *p5 = 0;               // For reference, this is a null pointer
void *p6 = NULL;            // For reference, this is a null pointer
void *p7 = nullptr;         // For reference, this is a null pointer (C++11)

static const int static_zero_1 = 0;       // Is this a literal zero when used?
static const int static_zero_2 = 1 - 1;   // No "literals 0" per se... is it?
void *p8 = (void *)static_zero_1;   // I have seen weird substitution rules...
void *p9 = (void *)static_zero_2;   // do they apply for NULL too?

P1 P2 ,而 P3 修改我加入 P8 P9 )会的空指针的(即 == NULL ,可能会或可能不会是零地址),其中这将与地址指针为零(可能是也可能不是 == NULL )?

which of p1, p2, and p3 (edit: I added p8 and p9) would be null pointers (i.e. == NULL, may or may not be address zero), and which of them would be pointers with the address zero (may or may not be == NULL)?

如果答案是C和C ++不同,它是什么在他们每个人的?

If the answer is different in C and C++, what is it in each of them?

推荐答案

P1 P2 是空指针; P3 是实现定义,
并可能是别的东西。 (逗号操作者不能成为其中的一部分
恒定的前pression。和非恒定的映射
整数值0到指针实现定义。)C是
与C ++在这里。

p1 and p2 are null pointers; p3 is implementation defined, and may be something else. (A comma operator cannot be part of a constant expression. And the mapping of a non-constant integral value 0 to a pointer is implementation defined.) C is identical to C++ here.

P8 P9 在C两个空指针++,但不是在C

p8 and p9 are both null pointers in C++, but not in C.

至于您的评论 static_zero_2 ,也没有
在任何语言要求的字面零是present,
任何地方。 G ++定义 NULL 因为编译器内置的 __空
例如,你可以使用(1 - 1)'\\ 0',或其他任何
恒恩pression评估为0。

With regards to your comment on static_zero_2, there is no requirement in either language that a literal zero be present, anywhere. g++ defines NULL as the compiler built-in __null, for example, and you can use (1 - 1), or '\0', or any other constant expression evaluating to 0.

这篇关于这些空指针,或者是他们的指针到地址0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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