指针运算在C void指针 [英] Pointer arithmetic for void pointer in C

查看:138
本文介绍了指针运算在C void指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个指向特定类型(比如 INT 字符浮动,..)递增,其值增加该数据类型的大小。如果无效指针,它指向大小的数据 X 递增,它是如何获得指向 X 字节前景如何?编译器如何知道要添加 X 指针的值?

When a pointer to a particular type (say int, char, float, ..) is incremented, its value is increased by the size of that data type. If a void pointer which points to data of size x is incremented, how does it get to point x bytes ahead? How does the compiler know to add x to value of the pointer?

推荐答案

最后得出结论:算术无效* 非法在C上和C ++。

Final conclusion: arithmetic on a void* is illegal in both C and C++.

GCC允许它作为一个扩展,请参阅算术>无效 - 和函数指针(注意,这部分是手工的C扩展一章的一部分)。锵和ICC可能允许无效* 算法的兼容性与海湾合作委员会的目的。其他的编译器(如MSVC)关于无效* ,和GCC不允许它不允许算术如果 -pedantic-错误标志被指定,或者如果 -Werror指针-ARITH 标志指定的(这个标志非常有用,如果你的code基还必须MSVC编译)。

GCC allows it as an extension, see Arithmetic on void- and Function-Pointers (note that this section is part of the "C Extensions" chapter of the manual). Clang and ICC likely allow void* arithmetic for the purposes of compatibility with GCC. Other compilers (such as MSVC) disallow arithmetic on void*, and GCC disallows it if the -pedantic-errors flag is specified, or if the -Werror-pointer-arith flag is specified (this flag is useful if your code base must also compile with MSVC).

报价从n1256草案中采取。

Quotes are taken from the n1256 draft.

加法运算的标准的说明指出:

The standard's description of the addition operation states:

6.5.6-2:对于加法,不是都是
  操作数应具有算术类型,
  或一个操作数应是一个指针
  对象类型和其他应
  有整型。

6.5.6-2: For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to an object type and the other shall have integer type.

所以,这里的问题是,是否无效* 是指向一个对象类型,或者等价地,无论是无效是一个对象类型。为对象类型的定义是:

So, the question here is whether void* is a pointer to an "object type", or equivalently, whether void is an "object type". The definition for "object type" is:

6.2.5.1:类型分成的对象类型的(类型,充分描述对象)的函数类型的(即描述函数类型),而不完全类型的。(描述对象,但缺乏必要的信息,以确定它们的大小类型)

6.2.5.1: Types are partitioned into object types (types that fully describe objects) , function types (types that describe functions), and incomplete types (types that describe objects but lack information needed to determine their sizes).

和该标准定义了无效为:

6.2.5-19:本的 无效 的类型包括:
  一个空值的集合;
  这是一个不完整的类型不能
  完成。

6.2.5-19: The void type comprises an empty set of values; it is an incomplete type that cannot be completed.

由于无效是一个不完整的类型,它不是一个对象类型。因此它不是一个有效的操作数的加法运算。

Since void is an incomplete type, it is not an object type. Therefore it is not a valid operand to an addition operation.

因此​​,你不能在执行无效指针运算指针。

Therefore you cannot perform pointer arithmetic on a void pointer.

最初,它被认为无效* 算术被允许,因为C标准的这些部分,

Originally, it was thought that void* arithmetic was permitted, because of these sections of the C standard:

6.2.5-27:作废的指针应有的一样重presentation和对齐
  要求的一个指针
  字符类型。

6.2.5-27: A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.

然而,

同样重presentation和对齐
  要求旨在暗示
  互换性作为参数
  从函数,返回值
  功能,和工会的成员。

The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.

因此​​,这意味着的printf(%S,X)具有相同的含义是否 X 的类型为的char * 无效* ,但并不意味着你可以做一个算术无效*

So this means that printf("%s", x) has the same meaning whether x has type char* or void*, but it does not mean that you can do arithmetic on a void*.

编者按:这个答案已被编辑,以反映最后的结论

Editor's note: This answer has been edited to reflect the final conclusion.

这篇关于指针运算在C void指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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