Netshroud Posted May 1, 2010 Share Posted May 1, 2010 (edited) How do I concatenate and pass strings? From what I understand, hardcoded strings have to be PSTR()'ed so that they end up on the Teensy, and they then run from program memory, such as: typeString( PSTR("Hello, World!") ); Reading that string seems to be painful. Currently, my typeString function is defines as: void typeString(const char* string); Except it appears not to be a standard character array. From what I've seen, char[0] should return 'H', but it isn't, and to return each character in the string, I need to use: char c; while ( c = pgm_read_byte(string++) ) { // Do Something } Which is complex enough for me. But how do I concatenate two strings, such as PSTR("Hello, ") and PSTR("World!") ? I'm currently trying: char* str1 = PSTR("Hello, "); char* str2 = PSTR("World!"); char* helloworld = malloc( strlen(str1) + strlen(str2) + 1); strcpy(helloworld, str1); strcat(helloworld, str2); This causes all code after this to not execute, leading me to think it's segfaulting. I've tried sprintf, but I get the following compiler error: strings.c:46: warning: implicit declaration of function 'sprintf' strings.c:64: warning: incompatible implicit declaration of built-in function 'sprintf' What am I doing wrong, and how do I do it right? ________________________________ Edit: OK, I've made some progress: PGM_P hello = PSTR("Hello, "); PGM_P world = PSTR("World!"); char* newstr = malloc( strlen_P(hello) + strlen_P(world) + 1); strcpy_P(newstr, hello); strcat_P(newstr, world); typeString(newstr); However this only prints out "World!". The first string is getting lost somewhere. Edited May 1, 2010 by Psychosis Quote Link to comment Share on other sites More sharing options...
Sl45h3R Posted May 1, 2010 Share Posted May 1, 2010 (edited) Sorry, didn't realize you were using C, please ignore this :) Edited May 1, 2010 by Sl45h3R Quote Link to comment Share on other sites More sharing options...
Netshroud Posted May 1, 2010 Author Share Posted May 1, 2010 Azn suggested in IRC simply typing two strings in a row, as concatenation is an expensive task. I may have to go with that. Quote Link to comment Share on other sites More sharing options...
Sl45h3R Posted May 1, 2010 Share Posted May 1, 2010 Can't you just do string a = "Hello, "; string b = "world!"; string hw = a+b; ? Quote Link to comment Share on other sites More sharing options...
Netshroud Posted May 1, 2010 Author Share Posted May 1, 2010 I wish. I honestly wish it was that simple. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.