C char to string (passing char to strcat ())

my problem is converting a char to a string, I have to go to strcat () a char to add to the string, how can I do this? thanks!

#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;
}
+5
source share
4 answers

Since it okpoints to an uninitialized array of characters, all this will be garbage values, therefore, when concatenation begins (through strcat), it is unknown. Also strcataccepts a C string (i.e. an array of characters that ends with the character '\ 0'). Giving char a[200] = ""will give you [0] = '\ 0', then from [1] to [199] set to 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;
}
+5

strcat . const char* ( C), . - :

char *asd(char* in, char *out)
{
    char *end = out + strlen(out);

    do
    {
        *end++ = *in;

    } while(*in++);

    return out;
}

do-while , C. , .

: , *in++;. in , , in++, * .

+3

, , , , C:

  • .
  • strcat .
  • - asd!
  • , char , .
#include <stdio.h>
#include <string.h>

int main(){
    char st[] = "text";
    char ok[200];
    ok[0] = '\0'; /* OR
    memset(ok, 0, sizeof(ok));
    */
    strcat(ok, st);
    printf("%s", ok);
    return 0;
}

, , , .

+1

, :

char* ctos(char c)
{
    char s[2];
    sprintf(s, "%c\0", c);
    return s;
}

: http://ideone.com/Cfav3e

0

All Articles