三个问题:是否定义了NULL-NULL?是否定义了(uintptr_t)NULL-(uintptr_t)NULL? [英] Three questions: Is NULL - NULL defined? Is (uintptr_t)NULL - (uintptr_t)NULL defined?

查看:86
本文介绍了三个问题:是否定义了NULL-NULL?是否定义了(uintptr_t)NULL-(uintptr_t)NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.是否定义了 NULL-NULL ??

1.Is NULL - NULL defined.?

  1. 是否已定义(char *)NULL-(char *)NULL .?

是否定义了(uintptr_t)NULL-(uintptr_t)NULL ?

我知道它可以在我所有的实现中使用.但是,从标准角度看,它是什么样的呢?我找不到明确的答案.

I know that it works in all used by me implementations. But how does it look like from the standard point of view? I cant find the clear answer.

从这个骗子中,我认为问题的一个答案是:是的.

From the dupe I assume that the question one answer is: YES.

第二个和第三个问题呢?

What about the second and third questions?

推荐答案

C标准将 NULL 定义为定义为宏的宏,该宏扩展为实现定义的空指针常量

The C Standard documents NULL as being defined as a macro which expands to an implementation-defined null pointer constant

根据实际定义,表达式 NULL-NULL 可能具有定义的值,也可能没有.例如:

Depending on the actual definition, the expression NULL - NULL may have a defined value or not. For example:

  • 如果将 NULL 定义为 #define NULL 0 ,则 NULL-NULL 实际上是类型为 int 的值为 0 .
  • 如果将 NULL 定义为 #define NULL((void *)0),则表达式 NULL 是一种违反约束的算法,因为没有在void指针上定义.
  • if NULL is defined as #define NULL 0, then NULL - NULL is actually a constant expression of type int with a value of 0.
  • if NULL is defined as #define NULL ((void *)0), the expression NULL is a constraint violation as arithmetic is not defined on void pointers.

第二个问题:是否定义了(char *)NULL-(char *)NULL (uintptr_t)NULL-(uintptr_t)NULL ?em>.这些表达式不再违反约束:

The second question: are (char*)NULL - (char*)NULL or (uintptr_t)NULL - (uintptr_t)NULL defined?. These expressions are no longer constraint violations:

  • 由于实现了从 void * 到算术类型的转换,因此(uintptr_t)NULL-(uintptr_t)NULL .在大多数当前系统上,它将为 0 ,但是C标准未对其进行定义.

  • since the conversion from void * to an arithmetic type is implementation defined, nothing can be said of the value of (uintptr_t)NULL - (uintptr_t)NULL. It will be 0 on most current systems, but the C Standard does not define it.

转换为(char *)稍有不同:仅当两个指针指向同一数组或指针的最后一个元素之后时,才定义两个指针的区别.数组,在此讨论中,一个对象被视为一个元素的数组.在(char *)NULL 中不是这种情况,它是一个空指针,因此不指向任何数组或对象.

converting to (char *) is a slightly different matter: the difference of 2 pointers is only defined if they point to the same array, or the position after the last element of the array, an object being considered an array of one element for this discussion. This is not the case in (char *)NULL, which is a null pointer, hence does not point to any array or object.

这篇关于三个问题:是否定义了NULL-NULL?是否定义了(uintptr_t)NULL-(uintptr_t)NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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