【问题标题】:warning C4047: '=': 'char' differs in levels of indirection from 'char [2]'警告 C4047:“=”:“char”的间接级别与“char [2]”不同
【发布时间】:2017-11-23 04:03:01
【问题描述】:

每当我编译我的程序时,我都会收到一个错误:警告 C4047。我对用这种语言进行编程非常陌生,不明白问题是什么或如何解决它。任何帮助将不胜感激,谢谢。

错误具体在word[x - 1] = "i";这一行

void RuleOne(char word[], char plural[]) {

    int x = strlen(word);

    word[x - 1] = "i";

    plural = strcat(word, "es");

}

【问题讨论】:

  • word[x - 1] = 'i'; 注意单引号。
  • 我去掉了C++标签,因为这个问题在C++中不存在。使用 C++,您将完全无法编译(编译错误),而不是警告。要使用 Microsoft 编译器编译为 C++,请将源代码文件从“.c”重命名为“.cpp”。
  • 请不要使用您使用的一种(或多种)语言以外的其他语言标记您的问题。它会误导那些通过谷歌等搜索引擎被带到这里的人。

标签: c


【解决方案1】:
word[x - 1] = "i";

"i"字符串文字,而不是字符常量

如果需要字符,请使用单引号:

words[x - 1] = 'i';
               ^ ^

此外,您使用plural 做错了。这是一个错误的工作:

plural = strcat(word, "es");

您实际上是将"es" 附加到word 并让指针plural 指向与word 相同的地址,这显然不是您打算做的。尝试将 word 附加 es 复制到副本:

strcpy(plural, word);
strcat(plural, "es");

由于strcpy()返回的是复制的字符串(缓冲区),你可以把它放在strcat()里面:

strcat(strcpy(plural, word), "es");

不过,我建议您在完全了解其工作原理之前不要这样做。

【讨论】:

  • @TchalyLeandreJr。点击旁边的灰色勾号,您可以随时接受您认为有用的答案。
【解决方案2】:

您的代码中有两个问题

1) 确实如 iBug 所述

words[x - 1] = 'i'; // not "i"

2) plural 没有设置为复数形式

// instead of >> plural = strcat(word, "es");
strcpy(plural, word);
strcat(plural, "es");

是你想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2019-06-21
    • 2016-08-02
    • 2018-10-09
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    相关资源
    最近更新 更多