By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,197 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,197 IT Pros & Developers. It's quick & easy.

Concatenating a single char from a structure to a string

P: 1
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
Jun 22 '07 #1
Share this Question
Share on Google+
2 Replies


mschenkelberg
P: 44
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
Jun 22 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
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:

Expand|Select|Wrap|Line Numbers
  1. string str;
  2. str += 'A';
  3.  
Jun 23 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.