"char" 类型的值不能用于初始化 "char *" 类型的实体
函数intStrAssign(HString&T,constchar*chars){//生成一个其值等于串常量chars的串Tintlen=strlen(chars);i...
函数
int StrAssign(HString &T, const char *chars) {
//生成一个其值等于串常量chars的串T
int len = strlen(chars);
int i;
if (T.ch)
T.ch =NULL;
if (!len) {
T.ch =NULL;
T.length =0;
}
else {
if (!(T.ch =(char *)malloc(len * sizeof(char))))
return OVERFLOW;
for (i =0; i < len; i++)
T.ch[i]=chars[i];
T.length=len;
}
return OK;
}
出错的主函数:
int main() {
char char1;
cout<<"请输入文本:"<<endl;
cin>> char1;
char *chars1 = char1; //char1下划波浪线,出现所述error
char *chars2 = "morn";
char *chars3 = "even";
HString s, v, t, sub;
int i;
StrAssign(s, chars1); //串s为:goodmorning
StrAssign(t, chars2); //串t为:morn
StrAssign(v, chars3); //串v为:even
for(i = 0; i < s.length; i++)
cout << s.ch[i]; //输出串s 展开
int StrAssign(HString &T, const char *chars) {
//生成一个其值等于串常量chars的串T
int len = strlen(chars);
int i;
if (T.ch)
T.ch =NULL;
if (!len) {
T.ch =NULL;
T.length =0;
}
else {
if (!(T.ch =(char *)malloc(len * sizeof(char))))
return OVERFLOW;
for (i =0; i < len; i++)
T.ch[i]=chars[i];
T.length=len;
}
return OK;
}
出错的主函数:
int main() {
char char1;
cout<<"请输入文本:"<<endl;
cin>> char1;
char *chars1 = char1; //char1下划波浪线,出现所述error
char *chars2 = "morn";
char *chars3 = "even";
HString s, v, t, sub;
int i;
StrAssign(s, chars1); //串s为:goodmorning
StrAssign(t, chars2); //串t为:morn
StrAssign(v, chars3); //串v为:even
for(i = 0; i < s.length; i++)
cout << s.ch[i]; //输出串s 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询