简体   繁体   English

将 char* 转换为 char* 小写时,不兼容的整数到指针转换从“int”分配给“char *”

[英]incompatible integer to pointer conversion assigning to 'char *' from 'int' when converting char* to char* lowercase

I have this annoying warning when I go to compile my code.当我去编译我的代码时,我有这个烦人的警告。 I'm trying to take a char * and convert it to lower case before I run it through a test.我正在尝试使用char *并将其转换为小写,然后再通过测试运行它。 My code compiles just fine but I want to learn how to get rid of this warning.我的代码编译得很好,但我想学习如何摆脱这个警告。 Other posts with this same error don't provide me with what I need to change to remove this error.具有相同错误的其他帖子没有为我提供我需要更改以删除此错误的内容。

//convert to lower case
char *c = "WORD TO LOWER";
char *s[testWordLen];
for (int i = 0; i < testWordLen; i++)
{
    s[i] =  tolower((unsigned char) c[i]);
}

An explanation of what I'm doing wrong would be great too.对我做错了什么的解释也会很棒。

You are creating a two-dimensional array using char *s[testWordLen];您正在使用char *s[testWordLen];创建一个二维数组char *s[testWordLen]; ; ; that is, a pointer to an array of chars.也就是说,一个指向字符数组的指针。 When you dereference it like this: s[i] = tolower((unsigned char) c[i]);当你像这样取消引用它时: s[i] = tolower((unsigned char) c[i]); , you assign a single character to an array. , 将单个字符分配给数组。

The fix is this: declare the variable as char s[testWordLen];解决方法是:将变量声明为char s[testWordLen];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:从“int”分配给“string”(又名“char *”)的指针转换不兼容的整数 - ERROR: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int' 错误:从“int”C分配给“string”(又名“char *”)的不兼容整数到指针转换 - error: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int' C C 中的结构错误“指向 integer 转换的不兼容指针从 'char [5]' [-Wint-conversion] 分配给 'char'” - Struct error in C "incompatible pointer to integer conversion assigning to 'char' from 'char [5]' [-Wint-conversion]" 指针转换不兼容的整数,将“ int”传递给类型为“ const char *”的参数 - Incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' 指针转换不兼容的整数,将“ char”传递给类型为“ const char *”的参数 - Incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *' 从“ int”类型分配给“ char * [12]”类型时不兼容的类型 - incompatible types when assigning to type 'char *[12]' from type 'int' 从int类型分配给char []类型时,类型不兼容 - Incompatible types when assigning to type char[] from type int 将Int转换为char *指针 - Converting Int to a char* pointer C指针转换不兼容的整数,将&#39;int&#39;传递给&#39;const char *&#39;类型的参数; - C incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *'; C:从char类型分配给char []类型时,类型不兼容* - C: Incompatible type when assigning to type char[] from type char *
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM