myworkmail 发表于 2009-12-23 23:45

再请教 程序匠人一个C语言的问题

再请教 程序匠人一个C语言的问题:
我在您的<C语言高效编程的的四招技巧>一文中看到这样的用法
const char string2 ="this is a example!";
char * cp;
cp = string2 ;
可编译器报错:
Error: 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

8# lxyppc

gshuang1 发表于 2010-3-1 10:07

晕,明显是类型不匹配,指针指个毛啊,这种小儿科问题都会犯,佩服!佩服!!

yt_shen 发表于 2010-4-19 14:27

const char string2 ="this is a example!";
const char * cp;
cp = string2 ;

zptonghua 发表于 2010-5-19 16:44

const char string2 ="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

那该怎么改呢
页: [1]
查看完整版本: 再请教 程序匠人一个C语言的问题