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

Question4.5:I have a char * pointer that happens to point to some ints

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-2 17:01:45 | 显示全部楼层 |阅读模式
Q: I have a char * pointer that happens to point to some ints, and I want to step it over them. Why doesn't
                          ((int *)p)++;
work?
A: In C, a cast operator does not mean ``pretend these bits have a different type, and treat them accordingly''; it is a conversion operator, and by definition it yields an rvalue, which cannot be assigned to, or incremented with ++. (It is either an accident or a deliberate but nonstandard extension if a particular compiler accepts expressions such as the above.) Say what you mean: use
                 p = (char *)((int *)p + 1);
or (since p is a char *) simply
                 p += sizeof(int);
or (to be really explicit)
                 int *ip = (int *)p;
                 p = (char *)(ip + 1);
When possible, however, you should choose appropriate pointer types in the first place, rather than trying to treat one type as another.
See also question 16.7.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

GMT+8, 2026-2-1 15:09 , Processed in 0.064645 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

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