gpt4 book ai didi

C 不兼容的整数到指针转换,将 'int' 传递给类型 'const char *' 的参数;

转载 作者:行者123 更新时间:2023-11-30 15:46:47 25 4
gpt4 key购买 nike

我试图从头开始创建一个 tolower 函数(出于学习目的),即使我转换了正确的类型,仍然会收到此错误:

alternate.c:13:18: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *'; [-Werror]
strcat(temp, (char) i + 32);
^~~~~~~~~~~~~
/usr/include/string.h:136:72: note: passing argument to parameter '__src' here
extern char *strcat (char *__restrict __dest, __const char *__restrict __src)
^
alternate.c:15:19: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *'; [-Werror]
strcat(temp, i);
^
/usr/include/string.h:136:72: note: passing argument to parameter '__src' here
extern char *strcat (char *__restrict __dest, __const char *__restrict __src)
^
alternate.c:18:17: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror]
strcat(temp, uppercase[i]);
^~~~~~~~~~~~
&
/usr/include/string.h:136:72: note: passing argument to parameter '__src' here
extern char *strcat (char *__restrict __dest, __const char *__restrict __src)
^
3 errors generated.

这是导致问题的代码:

char * toLowerCase(char * uppercase){
char * temp = "";
for(int i =0; i<strlen(uppercase); i++){

if( (int) uppercase[i] != 32){

if( (int) uppercase[i] < 97){
strcat(temp, (char) i + 32);
}else{
strcat(temp, i);
}
}else{
strcat(temp, uppercase[i]);
}
}
return temp;
}

最佳答案

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char *toLowerCase(const char *uppercase){
char *temp = malloc(strlen(uppercase)+1);

if(temp){
char *p = temp;
while(*p++ = tolower(*uppercase++));
/*
while(*uppercase){
*p++ = tolower(*uppercase++);
}
*p = '\0';
*/
}
return temp;
}

int main(void){
char *str = toLowerCase("ABC-XYZ");
printf("%s\n", str);//abc-xyz
free(str);
return 0;
}

关于C 不兼容的整数到指针转换,将 'int' 传递给类型 'const char *' 的参数;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18034446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com