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

标签 c strcat

我在使用下面的程序时遇到问题。我正在尝试扫描用户输入的特定单词的字符串命令。我现在的主要问题是,当我运行以下命令时,我收到一条警告,指出“传递 ‘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/

相关文章:

c - 指向数组的指针和动态内存分配

c - 为什么 write(2) 不返回 EINTR?

c++ - 冲突的枚举

c - c中无限字符串的strcat

c - Strcat 堆栈粉碎行为

c - 为什么这个简单的 strcat 在运行时会崩溃?

C++ 构建并发送 DNS 数据包

C - 堆上的缓冲区在 EXEC 后不会丢失,而是保存在堆栈上

c - 使用带有 char 指针的 strcat 函数

C 缩写程序返回 "Segmentation fault"