警告:指向整数转换的不兼容指针

warning: incompatible pointer to integer conversion

我正在尝试更改单词的最后一个字符,使其成为该单词的复数形式。例如巴士到巴士或便士到便士。当我尝试将最后一个字符设置为复数结尾时,我收到警告。请让我知道我需要更改什么才能解决此问题。谢谢。

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


int main(void) {
  
  char word[50];
  char *last_char;


  printf("Enter a word: "); 
  scanf("%[^\t\n]", word);
  last_char = &word[(strlen(word)-1)];
  

  if(*last_char == 'y')
  {
    *last_char = "ies";
    printf("\nthis does not work %s", word);
  }

  else if(*last_char == 's')
  {
    *last_char = "es";
    printf("\nthis is working %s", word);
  }

  else if(*last_char == 'h')
  {
    *last_char = 'es';
    printf("\nPlural: %s", word);
  }

  else 
  {
    last_char = &word[(strlen(word))];
    *last_char = 's';
    printf("\nPlural: %s", word);
  }

   
  return 0;
}

看看你自己说的:

I get a warning when I try to set the last character equal to the plural ending.

你在那个位置有一个角色的记忆槽,你正试图在那个槽中挤很多。行不通了。

"ies"; 不是单个字符,它是一个字符串 - 一个字符数组,它衰减到指向第一个字符的指针。单个char是整型;你不能将指针转换为字符(你可以,但它没有意义)。

在这种情况下,简单的解决方案是 strcpylast_character 开始的子数组的结尾,即:

strcpy(last_char, "ies");

至于

last_char = &word[(strlen(word))];
*last_char = 's';

这是不正确的,因为字符串不会是 null-terminated!

记住last_char已经指向最后一个字符,后面的空终止符在地址last_char + 1;您可以将 两行 行替换为

strcpy(last_char + 1, "s");