简体   繁体   English

C strcat-警告:传递'strcat'的arg 2会使指针从整数转换而无需强制转换

[英]C strcat - warning: passing arg 2 of `strcat' makes pointer from integer without a cast

I'm having a problem with the program below. 我下面的程序有问题。 I'm trying to scan through a string command entered by the user for certain words. 我正在尝试浏览用户输入的某些单词的字符串命令。 My major issue right now is that when I run the following I get a warning saying that "passing arg 2 of `strcat' makes pointer from integer without a cast". 我现在的主要问题是,当我运行以下命令时,我得到一个警告,即“传递'strcat'的arg 2会使指针从整数开始而没有强制转换”。 My intent is to loop through the first three characters of the string "s", concatenate them onto a string "firstthree", and later check the value of the string "firstthree". 我的意图是遍历字符串“ s”的前三个字符,将它们连接到字符串“ firstthree”,然后检查字符串“ firstthree”的值。 Any help is appreciated. 任何帮助表示赞赏。

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

Your program has a lot of problems; 您的程序有很多问题; you never initialize firstthree , for example. 例如,您永远不会初始化firstthree

The reason you're getting the specific error you're seeing is because of this call: 您收到所看到的特定错误的原因是由于此调用:

strcat(firstthree, s[i]);

s is a char * , so s[i] is a char , but strcat expects both parameters to be pointers to null-terminated strings. schar * ,所以s[i]char ,但是strcat期望两个参数都是指向以null终止的字符串的指针。 What it seems you want is something like: 您似乎想要的是这样的:

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

You can't use strcat() to do this; 您不能使用strcat()来执行此操作; it requires two char* s as arguments, not a char* and a char. 它需要两个char *作为参数,而不是char *和char。 You could use strncat() if it is available on your platform. 如果您的平台上提供了strncat(),则可以使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递 &#39;strcat&#39; 的参数 1 使指针从整数而不进行强制转换 + 指针和整数之间的比较 - passing argument 1 of 'strcat' makes pointer from integer without a cast + Comparison between pointer and integer 警告:传递 `atoi&#39; 的 arg 1 使指针从整数而不进行强制转换 - warning: passing arg 1 of `atoi' makes pointer from integer without a cast 传递&#39;memset&#39;的arg 2可以从指针进行整数转换而无需强制转换 - Passing arg 2 of `memset' makes integer from pointer without a cast 警告:传递“ quicksort”的参数1会使指针从整数开始而无需强制转换 - warning: passing argument 1 of 'quicksort' makes pointer from integer without a cast 警告:传递&#39;memcpy&#39;的参数1会使指针从整数开始而无需强制转换 - warning: passing argument 1 of ‘memcpy’ makes pointer from integer without a cast 警告传递&#39;finder&#39;的参数2使指针从整数开始而无需强制转换 - Warning passing argument 2 of 'finder' makes pointer from integer without a cast 警告:传递&#39;fopen&#39;的参数2会使指针从整数开始而无需强制转换 - warning: passing argument 2 of ‘fopen’ makes pointer from integer without a cast 警告:传递&#39;calcFx&#39;的参数2将使指针产生整数而不进行强制转换 - warning: passing argument 2 of 'calcFx' makes integer from pointer without a cast 警告:传递&#39;strcpy&#39;的参数1会使指针从整数开始而不进行强制转换 - warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast 传递参数 1 从指针生成整数而没有强制转换警告 - Passing Argument 1 makes integer from pointer without a cast warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM