找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 335|回复: 1

Question 11.24:Why can't I perform arithmetic on a void * pointer?

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-19 20:11:01 | 显示全部楼层 |阅读模式
Q: Why can't I perform arithmetic on a void * pointer? A: The compiler doesn't know the size of the pointed-to objects. (Remember that pointer arithmetic is always in terms of the pointed-to size; see also question 4.4.) Therefore, arithmetic on void *'s is disallowed (though some compilers allow it as an extension). Before performing arithmetic, convert the pointer either to char * or to the pointer type you're trying to manipulate (but see also questions 4.5 and 16.7).     

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-19 20:23:54 | 显示全部楼层
问题:为什么我不能对void *类型的指针进行算术操作?
回答:编译器不知道所指向的对象的大小(要记住指针运算总是要依赖所指向的对象的大小的;参考问题4.4)。因此基于void *类型的指针的运算是不被允许的(尽管一些编译器作为扩展是允许的)。在操作运算之前,要将指针要么转换为char *类型的或者你想要操作的指针的类型(也可以参考问题4.5和16.7)。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-2 04:22 , Processed in 0.077516 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表