C strcat – 警告:传递`strcat’的arg 2使得整数指针没有强制转换

我在下面的程序中遇到问题。 我正在尝试扫描用户为某些单词输入的字符串命令。 我现在的主要问题是,当我运行以下内容时,我会收到一条警告,说“传递`strcat’的arg 2会使整数指针没有强制转换”。 我的意图是遍历字符串“s”的前三个字符,将它们连接到字符串“firstthree”,然后检查字符串“firstthree”的值。 任何帮助表示赞赏。

#include  #include  #include  #include  #include  #include  #include  #include  #include  /* Simple example of using gnu readline to get lines of input from a user. Needs to be linked with -lreadline -lcurses add_history tells the readline library to add the line to it's internal histiry, so that using up-arrow (or ^p) will allows the user to see/edit previous lines. */ int main(int argc, char **argv) { char *s; while (s=readline("Enter Name: ")) { add_history(s); /* adds the line to the readline history buffer */ printf("Hello %s\n",s);/*output message to the user*/ char *firstthree; int i; for(i = 0; i < 3; i++){ strcat(firstthree, s[i]); printf("Hello %s\n",firstthree);//checking to see the character added to the end of the string } printf("Hey %s\n",firstthree);/*prints out the first three characters*/ free(s); /* clean up! */ free(firstthree); } return(0); 

}

你的程序有很多问题; 例如,你永远不会初始化第firstthree

你收到特定错误的原因是因为这个电话:

 strcat(firstthree, s[i]); 

s是一个char * ,所以s[i]是一个char ,但是strcat希望这两个参数都是指向以null结尾的字符串的指针。 你想要的东西是这样的:

 char firstthree[4] = { 0 }; for (int i = 0; i < 3; i++) { firstthree[i] = s[i]; printf("Hello %s\n", firstthree); } 

你不能用strcat()来做这件事; 它需要两个char * s作为参数,而不是char *和char。 如果您的平台上有strncat(),则可以使用它。