gpt4 book ai didi

C strcat - 警告 : passing arg 2 of `strcat' makes pointer from integer without a cast

转载 作者:太空宇宙 更新时间:2023-11-04 01:07:55 27 4
gpt4 key购买 nike

我在使用下面的程序时遇到问题。我正在尝试扫描用户输入的特定单词的字符串命令。我现在的主要问题是,当我运行以下命令时,我收到一条警告,指出“传递 ‘strcat’ 的 arg 2 使指针来自整数而不进行强制转换”。我的目的是遍历字符串“s”的前三个字符,将它们连接成一个字符串“firstthree”,然后检查字符串“firstthree”的值。感谢您的帮助。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>

/* 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);
}

最佳答案

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

您收到特定错误的原因是因为此调用:

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);
}

关于C strcat - 警告 : passing arg 2 of `strcat' makes pointer from integer without a cast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19307600/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com