5

my problem is in convert a char to string i have to pass to strcat() a char to append to a string, how can i do? 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;
}
frx08
  • 3,738
  • 7
  • 34
  • 44

4 Answers4

5

Since ok is pointing to an uninitialized array of characters, it'll all be garbage values, so where the concatenation (by strcat) will start is unknown. Also strcat takes a C-string (i.e. an array of characters which is terminated by a '\0' character). Giving char a[200] = "" will give you a[0] = '\0', then a[1] to a[199] set to 0.

Edit: (added the corrected version of the code)

#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;
}
legends2k
  • 27,643
  • 22
  • 108
  • 196
3

strcat will not append single characters. Instead it takes a const char* (a full C-style string) which is appended to the string in the first parameter. So your function should read something like:

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

    do
    {
        *end++ = *in;

    } while(*in++);

    return out;
}

The do-while loop will include the zero-terminator which is necessary at the end of C-style strings. Make sure that your out string is initialized with a zero-terminator at the end or this example will fail.

And as an aside: Think about what *in++; does. It will increment in and dereference it, which is the very same as in++, so the * is useless.

AndiDog
  • 62,237
  • 17
  • 152
  • 196
2

To look at your code, I can make a couple of pointers in relation to it, this is not a criticism, take this with a pinch of salt that will enable you to be a better C programmer:

  • No function prototype.
  • Incorrect usage of pointers
  • Dealing with the strcat function is used incorrectly.
  • Overdoing it - no need for the asd function itself!
  • Usage of dealing with variables notably char array that is not properly initialized.
#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;
}

Hope this helps, Best regards, Tom.

t0mm13b
  • 32,846
  • 7
  • 71
  • 106
  • Using the "asd" function allows for asd to be used in other contexts, allowing for main to ignore the computation behind asd itself. I'd always opt for a function, whatever the case might be. –  Feb 27 '10 at 13:56
  • Also, your memset() is completely irrelevant, simply declare it as so: char ok[200] = { 0 }; –  Feb 27 '10 at 13:56
  • @rogue: You can do it that way if you want, no explicit means, either your way or the way I have mentioned, notice the comment marker OR and the memset...either way depends on your style! – t0mm13b Feb 27 '10 at 14:05
  • Right guys, or you can do `ok[200] = "";` all-in-one statement. It's all a matter of style. – legends2k Feb 27 '10 at 14:48
  • thanks guys I appreciate your praise because I always try to understand how a programmer works because where I study, understanding is given all to the books and thanks to comments I can understand the most important part of the planning and everything you say I stock. However the example I did, I wrote the code only to give an example of my question, of course, to create a program I would never create and use a function like this ;) – frx08 Feb 27 '10 at 17:39
  • @legends2k: Don't you mean `char ok[200] = "";`? You won't be able to do that assignment anywhere other than the declaration of ok. – tomlogic Feb 27 '10 at 18:20
  • @tomlogic: Of course, I meant declaration + initialization; that's why in that comment I had written "all-in-one". – legends2k Feb 28 '10 at 11:25
0

To convert a character to a (null terminated) string you could simply do:

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

Working example: http://ideone.com/Cfav3e

Joseph
  • 569
  • 1
  • 5
  • 12
  • Does not work since `c` is still of type `char`. `sprintf` expects to work with strings. – skytreader Feb 11 '14 at 05:50
  • This appears to work, actually. `sprintf` can take a character argument if it's filling in a `%c` in the format string. – Dave Yarwood Dec 23 '15 at 16:01
  • It's a nice way to do the conversion, but: (1) you are returning a local variable, which should really crash, and (2) the embedded NUL in the format string must be omitted. – barfuin Mar 18 '21 at 09:30