[匠人手记] 再请教 程序匠人一个C语言的问题

[复制链接]
6715|14
 楼主| myworkmail 发表于 2009-12-23 23:45 | 显示全部楼层 |阅读模式
再请教 程序匠人一个C语言的问题:
我在您的<C语言高效编程的的四招技巧>一文中看到这样的用法
const char string2[len] ="this is a example!";
char * cp;
cp = string2 ;
可编译器报错:
Error[Pe513]: a value of type "unsigned char const *" cannot be assigned to an entity of type "unsigned char *" D:\work\vfddiplay20091023(v2.0)\program\display2.01\Project\Template\User\Main\main.c 118

怎么处理
 楼主| myworkmail 发表于 2009-12-23 23:46 | 显示全部楼层
在IAR FOR ARM 4.42下
 楼主| myworkmail 发表于 2009-12-23 23:53 | 显示全部楼层
应该是类型不匹配
程序匠人 发表于 2009-12-24 00:01 | 显示全部楼层
拜托在提问前,先确定作者。
 楼主| myworkmail 发表于 2009-12-24 18:14 | 显示全部楼层
不好意思,那就是我搞错,那能指点一下吗?
lxyppc 发表于 2009-12-24 20:08 | 显示全部楼层
晕,不是吧
这样写如果编译通过了,怎么死的都不知道

将const指针强制指向非const指针很危险的,也不知道是谁想的这么个主意
如果非要这么写,那就写:
cp = (char*)string2
 楼主| myworkmail 发表于 2009-12-24 20:22 | 显示全部楼层
我查了,好象是程序匠人写的 .哦也
http://blog.21ic.com/user1/349/archives/2008/48016.html
lxyppc 发表于 2009-12-24 21:00 | 显示全部楼层
哦也
真是匠人
看来可以改改了 :lol
rain_tz 发表于 2010-1-6 20:10 | 显示全部楼层
不太懂,路过
momonivvuu 发表于 2010-1-27 16:19 | 显示全部楼层
gshuang1 发表于 2010-3-1 10:07 | 显示全部楼层
晕,明显是类型不匹配,指针指个毛啊,这种小儿科问题都会犯,佩服!佩服!!
yt_shen 发表于 2010-4-19 14:27 | 显示全部楼层
const char string2[len] ="this is a example!";
const char * cp;
cp = string2 ;
zptonghua 发表于 2010-5-19 16:44 | 显示全部楼层
const char string2[len] ="this is a example!";
char * cp;
cp = string2 ;
在ANSI c中式不允许出现这样的情况的,在C++里面是可以的
xyp749192072 发表于 2010-5-27 18:03 | 显示全部楼层
1# myworkmail 定义的指针是变量,数组地址在编译时候已经确定,不是变量,所以不能这么操作。
28182900 发表于 2012-6-28 13:32 | 显示全部楼层
那该怎么改呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

22

主题

100

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部