【问题标题】:C incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *';C 不兼容的整数到指针的转换将“int”传递给“const char *”类型的参数;
【发布时间】:2013-08-03 15:37:27
【问题描述】:

我正在尝试从头开始制作一个 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;
}

【问题讨论】:

  • char != char *。你strcat 是一个字符串,而不是单个字符。
  • 您的temp 变量只有足够的空间来存储单个字符。你需要更多。提示1:至少strlen(uppercase) long。提示 2:你不能使用 local 变量,因为你不能返回它。
  • temp 是字符串字面量,你尝试用strcat修改它
  • 对不起,我是 c 新手,你能详细说明一下你是如何分配内存的吗?是否可以将字符附加到字符串?
  • 如果在 C 中请阅读手册并确保数据类型与签名匹配。因此,当使用整数而不是字符串时,需要一些未定义的东西。还学习使用调试器。

标签: c casting


【解决方案1】:
#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;
}

【讨论】:

  • 您正在检查 toLowerCase 中的 NULL 并在不取消引用的情况下返回它,但是在将 toLowerCase 的结果传递给 printf 之前,您没有检查 NULL。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 2015-03-08
  • 1970-01-01
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多