Concatenating a single char from a structure to a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Oki
    New Member
    • Jun 2007
    • 1

    Concatenating a single char from a structure to a string

    I appreciate any help; I'm currently getting a problem with trying to concatenate a single char from a structure to an existing string. Basically, the problem is here:

    strcat( res, morse_c[j].letter )

    morse_c[j].letter should return an alphabet 'a' or 'b', etc...
    I'm trying to concatenate that to a char *res and it returns the error:

    'strcat' : cannot convert parameter 2 from 'char' to 'const char *'

    I"ve tried casting it --> (const char*) morse_c[j].letter AND forcing the alphabets to const char type by using double quotes "a", "b", etc...

    None worked. I've also tried setting res[i] = morse_c[j].letter and it doesn't work. When I return res, it only returns the first value res[0] and not the whole res string

    Thank you!

    PS: I'm thinking of changing my structure of letter to const char* but that will be a mess.

    Code snipets:

    char *res = (char *)calloc(200, sizeof(char));

    Structure data:
    typedef struct {
    char letter;
    char sequence[100];
    } morse_t;

    morse_t* codeGen(){
    morse_t *res = (morse_t*) calloc(TABLE_SZ , sizeof(morse_t) );
    res[0] = make_morse( 'a', ".-" );
    res[1] = make_morse( 'b', "-...");
    res[2] = make_morse( 'c', "-.-.");
    res[3] = make_morse( 'd', "-..");
    ... until the table is populated
  • mschenkelberg
    New Member
    • Jun 2007
    • 44

    #2
    try overloading the += operator and just resize the char * res and add the letter on the end, and don't forget to account for the null

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      strcat appends one string to another string. A char is not a string.

      Create a string with your char and a \0.

      If you are using C++, then do not use strcat. Use string:

      [code=cpp]
      string str;
      str += 'A';
      [/code]

      Comment

      Working...