一.问题描述
1.题目所示报错意思为:
const char[18]对char[200]赋值时类型不兼容。
2.报错原因:
将一个常量字符数组(const char [21])赋值给一个非常量字符数组(char [200]),在C++中,常量字符数组的内容是不可修改的,而非常量字符数组的内容是可以修改的。
如下错误代码:
#include<iostream>
#include<cstring>
int main(){
char temp[200];//非常量字符串
temp="l1xAQu0GHJKLNBHUL";//常量字符串
for(int i=strlen(temp)-1;i>=0;i--){
printf("%c",temp[i]);
}
return 0;
}
二.解决方案
#include<iostream>
#include<cstring>
int main(){
const char temp[]="l1xAQu0GHJKLNBHUL";
for(int i=strlen(temp)-1;i>=0;i--){
printf("%c",temp[i]);
}
return 0;
}