从`字符无效的转换'为'字符*' [英] invalid conversion from `char' to `char*'
问题描述
code是:
INT的main(){ INT I,最后的;
焦炭TXT [500]; 的printf(联合国Donnez为例\\ n吗?);
得到(TXT);
最后= strlen的(TXT); 对于(i = 0; I< 50;我++){
如果(我== 0){
的strcpy(TXT [1],TXT [0]);
}其他{
的strcpy(TXT [Ⅰ-1],TXT [I]);
}
} 残培();
返回0;
}
错误行:的strcpy(TXT [1],TXT [0]);
有什么问题的原因是什么?
的的strcpy
函数有两个的char *
(技术上,一个的char *
和为const char *
)。此外,不允许通过的strcpy
重叠的缓冲区。
看起来你想写
TXT [I-1] = TXT [I]
(这将删除该字符串开头的字符)。
为(I = 1 / *之一,不为零* /; I< 50;我++){
TXT [I-1] = TXT [I];
}
不错,但我想用
的strcpy
。怎么样?
块引用>您不允许使用
的strcpy
无需中间缓冲器。如果必须使用的strcpy
,像这样做:字符TXT [500],TMP [500];
的printf(联合国Donnez为例\\ n吗?);
与fgets(TXT,499,标准输入);
的strcpy(TMP,&放大器; TXT [1]); //注意'for'循环不再需要code is :
int main() { int i,last; char TXT[500]; printf("Donnez un exemple ?\n"); gets(TXT); last = strlen(TXT); for(i=0;i<50;i++){ if (i==0){ strcpy(TXT[1],TXT[0]); } else { strcpy(TXT[i-1],TXT[i]); } } getch(); return 0; }
error in line :
strcpy(TXT[1],TXT[0]);
What is the cause of the problem ?
解决方案The
strcpy
function takes twochar*
(technically, achar*
and aconst char*
). Moreover, it is not allowed to passstrcpy
overlapping buffers.It looks like you wanted to write
TXT[i-1] = TXT[i];
(this would delete the leading character from the string).
for(i=1 /* one, not zero */ ; i<50 ; i++) { TXT[i-1] = TXT[i]; }
Good, but I want to use
strcpy
. How?You are not allowed to use
strcpy
without an intermediate buffer. If you must usestrcpy
, do it like this:char TXT[500], TMP[500]; printf("Donnez un exemple ?\n"); fgets(TXT, 499, stdin); strcpy(TMP, &TXT[1]); // Note that 'for' loop is no longer required
这篇关于从`字符无效的转换'为'字符*'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!