无法将strcmp的参数1从char转换为const char
我刚开始在c中使用指针。我想通过在命令提示符中输入字符串来对字符串进行排序 当我尝试实现它时,会出现以下错误:无法将strcmp的参数1从char转换为const char,c,C,我刚开始在c中使用指针。我想通过在命令提示符中输入字符串来对字符串进行排序 当我尝试实现它时,会出现以下错误: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *' 你能指导我,告诉我如何用更少的指针和更多的数组来编写代码吗 char *sort(char *sortIt) { char *p =sortIt; //p = (char*)calloc(sizeof(char)); char
'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
你能指导我,告诉我如何用更少的指针和更多的数组来编写代码吗
char *sort(char *sortIt)
{
char *p =sortIt;
//p = (char*)calloc(sizeof(char));
char temp[3];
int len = strlen(sortIt);
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if(strcmp(p[i],p[j])>0)
{
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
for(int ij=0;ij<len;ij++)
printf("%s", p[ij]);
}
int main(char argc, char **argv)
{
char *p=argv[1];
sort(p);
}
char*排序(char*sortIt)
{
char*p=sortIt;
//p=(char*)calloc(sizeof(char));
炭温[3];
int len=strlen(sortIt);
对于(int i=0;i编辑:
改变一切
p[i], p[j]
到
因为您需要常量字符指针。在最初的情况下,您只需要查看字符。在C中,字符串表示以null结尾的字符串。因此大多数C库函数都利用了这一点。例如,strcmp
,strcpy
,等等
因此,您的代码在调用strcmp(p[i],p[j])
和strcpy(temp,p[i])时是错误的,因为strcmp
和strcpy
处理string
而不是chars
和p[i]
是char
要比较C中的字符,您可以只使用
if(p[i]>p[j])…
。要复制字符,您可以只使用char C=p[j];
似乎代码正在尝试对字符串中的char
进行排序。代码中记录了更正和想法
char *sort(char *sortIt)
{
// After assignment p & sortIt never change. Only need one of these.
char *p =sortIt;
// casting the return of callloc() and malloc() is frowned upon.
// Note: sizeof(char) is _always_ 1
//p = (char*)calloc(sizeof(char));
char temp[3]; // Only 1 char is needed. Best to declare in inner lock
int len = strlen(sortIt); // Better to use size_t len,i,j,ij;
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
// Only need to compare p[i] > p[j]
if(strcmp(p[i],p[j])>0)
{
// Only need to swap p[i] p[j]
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
// Only need to print the string once
for(int ij=0;ij<len;ij++)
printf("%s", p[ij]);
// missing return
}
您可以发布错误吗?您正在混淆字符串和单个
char
s,错误消息非常清楚。请先阅读strcmp
和strcpy
手册。将char*p
更改为const char*p
怀疑您想要if(strcmp(&p[i],&p[j])>0)
。但还有很多其他问题。如果要比较单个字符,请不要使用strcmp()
。只要使用相等的=
,小于
或大于code>,这取决于您的排序需要。strcmp
希望您提供一个const char,您从哪里了解到的?const char,我指的是const char指针,当然这不是我的否决票,但我要说的明显原因是您的答案不正确帮助。如果temp是一个constchar[]
,那么strcpy(temp,p[i]);
将如何工作?如果你想把答案改为正确,请关注p[i]
…什么是p[i]
为什么这会导致strcpy出现问题呢?您可以将字符*
传递给需要常量字符*
的函数。请阅读中的示例,它是否使用了常量
?仍然错误。添加和可能会使程序编译,但仍然不正确(temp
如果输入字符串长度超过两个字符,将溢出)。在C语言中,字符串总是表示以空字符'\0'
结尾的字符串。如果没有'\0'
,它只是字符的数组或指向字符的指针。
@John Smith。你能告诉我应该更改什么吗?在“我发现了问题”之后,你想对代码块说什么?它正在调用memcpy
,目标值为未初始化值。@Andrew Medico ooops!。编辑错误删除所需的q=malloc(大小)
;
char *sort(char *sortIt)
{
// After assignment p & sortIt never change. Only need one of these.
char *p =sortIt;
// casting the return of callloc() and malloc() is frowned upon.
// Note: sizeof(char) is _always_ 1
//p = (char*)calloc(sizeof(char));
char temp[3]; // Only 1 char is needed. Best to declare in inner lock
int len = strlen(sortIt); // Better to use size_t len,i,j,ij;
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
// Only need to compare p[i] > p[j]
if(strcmp(p[i],p[j])>0)
{
// Only need to swap p[i] p[j]
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
// Only need to print the string once
for(int ij=0;ij<len;ij++)
printf("%s", p[ij]);
// missing return
}
char *sort(char *p) {
size_t len = strlen(p);
for (size_t i = 1; i < len; i++) {
for (size_t j = 0; j < i; j++) {
/// As this code changed the order of indexing, maybe p[i] < p[j]
if (p[i] > p[j]) {
char temp;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
printf("%s\n", p); // Recommend adding \n
return p;
}
int main(char argc, char **argv)
{
// Good to test if argv[1] is valid.
if (argc <= 1) return 1;
// Although legal for historic reasons, best to
// char *p=argv[1];
const char *p=argv[1];
// Since sort() is going to re-arrange p, either sort() allocates new memory
// or we allocate memory here. Then const char *p is not needed.
size_t size = strlen(argv[1]) + 1;
char *q;
q = malloc(size);
memcpy(q, argv[1], size);
// sort(p);
sort(q);
// Good policy to free allocated memory
free(q);
return 0; // Always good to return a value from main()
}