C char to string(将char传递给strcat())

frx*_*x08 5 c string char strcat

我的问题是将char转换为字符串我必须传递给strcat()一个字符串附加到字符串,我该怎么办?谢谢!

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

char *asd(char* in, char *out){
    while(*in){
        strcat(out, *in); // <-- err arg 2 makes pointer from integer without a cast
        *in++;
    }
    return out;
}

int main(){
    char st[] = "text";
    char ok[200];
    asd(st, ok);
    printf("%s", ok);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

leg*_*s2k 5

由于ok指向未初始化的字符数组,因此它们都是垃圾值,因此串联(by strcat)将在何处开始是未知的.还strcat接受一个C字符串(即由'\ 0'字符终止的字符数组).给char a[200] = ""会给你一个[0] = '\ 0',则[1]至[199]设置为0.

编辑:(添加了更正的代码版本)

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

char *asd(char* in, char *out)
{

/*
    It is incorrect to pass `*in` since it'll give only the character pointed to 
    by `in`; passing `in` will give the starting address of the array to strcat
 */

    strcat(out, in);
    return out;
}

int main(){
    char st[] = "text";
    char ok[200] = "somevalue"; /* 's', 'o', 'm', 'e', 'v', 'a', 'l', 'u', 'e', '\0' */
    asd(st, ok);
    printf("%s", ok);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)