我可以做用C void *指针算术? [英] Can I do arithmetic on void * pointers in C?

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

问题描述

这是有效的。

void *p = &X; /* some thing */
p += 12;

如果是的话是什么P中就指向?
我有(第三方)code,这是否(以及完全编译),我的猜测是无效*被视为一个char *。我信赖的K&安培; R是沉默的话题(ISH)

and if so what does p now point to? I have (third party) code that does this (and compiles cleanly) and my guess is that the void * was treated as a char *. My trusty K&R is silent(ish) on the topic

编辑:我的小测试应用程序运行在GCC 4.1.1罚款和治疗无效*为char *。但G ++ barfs

My little test app runs fine on gcc 4.1.1 and treats void * as char *. But g++ barfs

我知道如何正确地做到这一点。我需要知道,如果我有擦拭本code碱基找到所有的地方它完成。

I know how to do it properly. I need to know if I have to clean this code base to find all the places its done.

BTW的gcc -pedantic抛出了一个警告

BTW gcc -pedantic throws up a warning

摘要:

的C规格不明确。它说,在重新presentation的条款及功能参数无效*​​ =字符使用*。但它是无声的对指针运算。

The C spec is ambiguous. It says that in terms of representation and use as function parameters void* =char*. But it is silent regarding pointer arithmetic.


  • GCC(4)允许的话,将其视为
    字符*

  • 先按g ++拒绝它

  • GCC -pedantic发出警告它

  • VS2010 C和C ++
    拒绝它

推荐答案

这取决于编译器。那些允许它考虑的sizeof(*(无效*))为1。

It depends on compiler. Those that allow it consider sizeof(*(void *)) as 1.

编辑:这只是对空指针运算。它将使用的sizeof(INT)的这种情况下,步骤或0人谁使用它会是最小的可能步骤的共同期待都没有任何意义。

it's only for void pointer arithmetic. It would have no sense using in this case steps of sizeof(int) or of 0. The common expectations of someone who uses it would be the smallest possible step.

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

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