cannot convert from 'const char [3]' to 'char'

作者&投稿:钟离厘 (若有异议请与网页底部的电邮联系)
cannot convert from 'const char *' to 'char *'~

强制转换下,就ok了


#include
void main( void )
{
const char *str1="hello";
char *str2;
str2=(char *)str1; //////强制转换下,就ok了
}

char expression[]等价于char * expression,而'('属于字符常量,这个赋值显然是不行的。可以将之改为char expression[]="(";

回二楼friday_collin,*lp++ = 'A'并不是“表达式做左值”。
这样完全可以的:
main()
{
int a[10]={0},*p=a;
*p++=2;
}

但这样不行:
main()
{
int p=0;
p++=2;
}

cannot convert from 'const char [3]' to 'char'
这个错误的关键,是不能把字符串"■"转换成字符!

void PrintMaze (maze_t maze, int width, int height)
{
int w, h;
char *line, *lp;

line = (char *) calloc ((width + 1) * 2, sizeof (char));
if (line == NULL) {
(void) fprintf (stderr, "Cannot allocate memory!\n");
exit (EXIT_FAILURE);
}
maze->up = TRUE;
(maze + (width * height) - 1)->down = TRUE;

for (lp = line, w = 0; w < width; w++) {
lp=(char *) malloc(10*sizeof(char)); /////这里申请了内存
strcpy(lp,"■"); ///////////////////////这里进行了赋值
if ((maze + w)->up)
*lp++ = ((maze + w)->path) ? '.' : ' '; //////再赋值,回把刚才的赋值覆盖掉,应该用strcat追加
else
lp=(char *) malloc(10*sizeof(char)); /////这里怎么又申请了?
strcpy(lp,"■"); /////////应该用strcat追加
}
lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");
(void) puts (line);
for (h = 0; h < height; h++) {
for (lp = line, w = 0; w < width; w++) {
if ((maze + w)->left)
*lp++ = ((maze + w)->path && (maze + w - 1)->path) ? '.' : ' ';
else
lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");
*lp++ = ((maze + w)->path) ? '.' : ' ';
}
lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");
(void) puts (line);
for (lp = line, w = 0; w < width; w++) {
lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");
if ((maze + w)->down)
*lp++ = ((maze + w)->path && (h == height - 1 ||
(maze + w + width)->path)) ? '.' : ' ';
else
lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");
}
lp=(char *) malloc(10*sizeof(char));
strcpy(lp,"■");
(void) puts (line);
maze += width;
}
free (line);

}/* PrintMaze */

非常感谢楼下的指点,呵呵 我原来以为表达式不可以做左值,虽然这句话是正确的,但是指针的情况特殊哦,呵呵 受教了,谢谢
我看别人做迷宫的时候的方块都是调用系统函数画二维条形图,我这共有个迷宫的程序,你有兴趣看看(学术交流,如果你的问题解决了不妨给我发个消息我也学习下,呵呵)(这个是在TC下做的):
#include"graphics.h"
#include"dos.h"
#include"conio.h"
#include"stdlib.h"
main()
{
int gdriver,gmode;
gdriver=DETECT;
detectgraph(&gdriver,&gmode);
registerbgidriver(gdriver);
initgraph(&gdriver,&gmode,"c:\\tc");
mainpicture();
muse();
migong();
getch();
}
/*==========================mainpicture()===================================*/
mainpicture()
{
char *title="=CYH-ZHL=";
char *love="LOVE AT FIRST SIGHT";
int i;
setfillstyle(1,8);
bar(50,10,580,450);
setfillstyle(7,2);
bar(55,15,575,445);
setfillstyle(1,3);
bar(60,20,570,440);
setfillstyle(1,0);
bar(90,50,540,410);
settextstyle(0,0,3);
setcolor(8);
for(i=0;i<5;i++)
outtextxy(200+i,25+i,title);
setcolor(4);
outtextxy(200,25,title);
setcolor(0);
settextstyle(0,1,2);
outtextxy(85,70,love);
outtextxy(565,70,love);
}
/*=================================muse()===================================*/
muse()
{
setfillstyle(1,15);
bar(110,60,520,90);
setfillstyle(1,8);
bar(112,62,518,88);
setfillstyle(1,3);
bar(120,68,180,82);
settextstyle(0,0,1);
setcolor(15);
outtextxy(127,72,"MiGong");
}
/*===================================migong()====================================*/
migong()
{
int x,y,z;
char a;
int key;
int px=1,py=1;
int abc[20][40]={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,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1,0,1},
{1,0,1,0,1,0,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,1,1,1,1,1},
{1,0,1,0,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0,1,1,0,1,0,0,0,1,0,0,0,0,0,1},
{1,0,1,1,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,1,1,0,1,0,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
for(x=0;x<20;x++)
{
for(y=0;y<40;y++)
{
setfillstyle(1,0);
if(abc[x][y]==1) bar(115+10*y,100+10*x,115+10*y+10,100+10*x+10);
settextstyle(0,0,1);
setcolor(15);
outtextxy(480,208,"EXIT");
}
}
rectangle(110,95,520,400);
rectangle(118,105,512,300);
rectangle(118,310,512,390);
outtextxy(130,315,"Esc Exit");
outtextxy(130,325,"^ Up");
outtextxy(130,335,"v Down");
outtextxy(130,345,"< Left");
outtextxy(130,355,"> Right");
setfillstyle(1,12);
bar3d(126,111,134,119,5,1);
for(;;)
{
key=bioskey(0);
if(key==0x11b) break;
if(key==0x4d00)
{
if(py+1<40 && abc[px][py+1]!=1)
{
setfillstyle(1,10);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
py=py+1;
setfillstyle(1,12);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
if(px==11 && py==36) migongimage();
}
}
if(key==0x4b00)
{
if(py-1>-1 && abc[px][py-1]!=1)
{
setfillstyle(1,10);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
py=py-1;
setfillstyle(1,12);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
if(px==11 && py==36) migongimage();
}
}
if(key==0x5000)
{
if(px+1<20 && abc[px+1][py]!=1)
{
setfillstyle(1,10);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
px=px+1;
setfillstyle(1,12);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
if(px==11 && py==36) migongimage();
}
}
if(key==0x4800)
{
if(px-1>-1 && abc[px-1][py]!=1)
{
setfillstyle(1,10);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
px=px-1;
setfillstyle(1,12);
bar3d(115+10*py+1,100+10*px+1,115+10*py+10-1,100+10*px+10-1,5,1);
if(px==11 && py==36) migongimage();
}
}
}
}
/*========================================migongimage()===========================*/
migongimage()
{ setcolor(10);
settextstyle(0,0,3);
delay(2000);
rectangle(252,332,283,368);
delay(2000);
outtextxy(257,340,"G");
line(283,332,313,368);
line(283,368,313,332);
delay(2000);
rectangle(313,332,344,368);
delay(2000);
outtextxy(318,340,"O");
line(344,332,374,368);
line(344,368,374,332);
delay(2000);
rectangle(374,332,405,368);
delay(2000);
outtextxy(379,340,"O");
line(405,332,435,368);
line(405,368,435,332);
delay(2000);
rectangle(435,332,466,368);
outtextxy(440,340,"D");
}

首先,#include <string.h>

将下面这一句:
*lp++ = "■";

改为:
strcpy(lp,"■");

一点建议:
对于lp增加一下malloc是否成功的容错处理

==============================================
补充答复:
原来是做迷宫啊,迷宫的方块不是直接用"■"来堆建的,
对于编译器来说,"■"就是一个3字节的char数据,可不是我们肉眼看到的小方块。

迷宫的方块是通过以下方法画的:
相关头文件:#include<graphics.h>
涉及接口:
setfillstyle(int pattern, int color); //设置填充图样和颜色函数

例如:
setfillstyle(SOLID_FILL,RED);

不能把这个和那个对调


资阳市17784353322: C++编译提示“cannot convert from 'int' to 'int ** '” -
谈坚茵莲: &p = a 指针p本来存储的就是地址,&p又是什么?写成*p=a就对了,意思是将整数a赋给p所指向的地址内

资阳市17784353322: C语言错误error C2440: '=' : cannot convert from 'void' to 'int'是什么问题? -
谈坚茵莲: 这是把void类型的值赋给int变量 当然不可以了 比如一个void函数A void A(){} int变量 x int x;x=A();就会出现

资阳市17784353322: Java 编程出错 cannot convert from int to boolean 怎样解决 -
谈坚茵莲: public class Main { static String Str( String A,String B,int Inte ){if( Inte == 1 ){return A + B;} return "";} public static void main(String[] args){System.out.println( Str("aaa","bbb",1));}}if( Inte == 1 ) 而不是 if( Inte = 1 ) ...一个等号是...

资阳市17784353322: 编译出错cannot convert from 'char [1]' to 'char' -
谈坚茵莲: 不能将 "const char *" 类型的值分配到 "char" 类型的实体 初始化字符数组 用char chemical[100]="";即可.

资阳市17784353322: 出现error C2440: '=' : cannot convert from 'double' to 'double [5]'错误 -
谈坚茵莲: 出错报告说是赋值时发生错误,无法将整型数据转化为整型指针变量. #include using namespace std; void main() { int a[10],i,*p; cout>a[i]; p=a; for(i=1;i

资阳市17784353322: cannot convert from 'void *' to 'int *' -
谈坚茵莲: int delta(int n); //函数声明要返回int类型 但是你的函数体里面没有return 语句返回一个int类型的数据,所以会报错

资阳市17784353322: eclipse中for循环Type mismatch: cannot convert from int to boolean -
谈坚茵莲: java中for循环的格式是for(变量;布尔表达式;变量增减表达式).就是说for(?;?;?){}中的三个参数第一个是一个变量第二个是一个执行结果为布尔类型用来限制循环的范围的表达式第三个是该变量的增减操作用来计算循环步长的.按照上面的代码编译都是问题

资阳市17784353322: java代码报cannot convert from char[] to String -
谈坚茵莲: 一个字符串怎么可能同时又是char[]呢?改为char[] array = paramString.toCharArray();

资阳市17784353322: error C2440: '=' : cannot convert from 'struct LNode *' to 'int *'这句话是什么意思 -
谈坚茵莲: 出现这种情况的原因是,你参数赋值时,等号左右两边的参数类型不对. 根据错误描述 等号左边的参数类型是int*, 等号右边的参数类型是LNode*. 这两个肯定不能相等. 估计你是把LNode指针指针直接给了int指针,应该是LNode指针所指对象的一个参数给int指针.

资阳市17784353322: cannot convert from 'const int' to 'int [3][4]' -
谈坚茵莲: 改成: int a[]={2,3,4}就可以了.因为定义的是指针而赋值的是整型数组.可以定义指针来指向整型数组,但是不能用整型数组来初始化一个整型指针.可以这样用:int a[]={3,4,5}; int *pInt = a; for( int i = 0 ; i < 3 ; i++) { printf("%d",*pInt); pInt++; }

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网