adding text to char *

15 Nov 2012

I'm a new at the c++ language, so here's a real newbee question...

I'm trying to combine a char pointer with a line of text and store it again in the char pointer. But i can't figure out how to do it.

int main() {
    char *response = "test";
    *response = "this is a " & *response;
    pc.printf("Response=%s\r\n", response);
  
}

This compiles the error: expression must have integral or enum type.

I can't change the variable type to string or something like that, it has to be a char * variable (to use with other functions) How do i solve this?

Thanks in advance!

15 Nov 2012

You cannot add char arrays like that. Two ways to do it, you can do it manually, or using sprintf, which is just like printf but stores it in another char array.

So in your case:

#include "mbed.h"

int main() {
    char buffer[20];
    char *response = "test";
    sprintf(buffer, "this is a %s ", response);
    printf("Response=%s\r\n", buffer);
  
}

In your code you added it to the original char response, so I did that also at first, wondering why it didnt work. But the original response array has a certain length, if you add the other text to it you get a buffer overflow, corrupting the program. You can do that, but then you have to define response as a larger buffer.

15 Nov 2012

Or like this:

#include "mbed.h"

int main() {
    char buffer[ 20 ];
    char *response = "test";
    strcpy( buffer, "this is a " );
    strcat( buffer, response );
    printf ("Response=%s\r\n", buffer );
}

The strcpy (string copy) and strcat (string concatenate) functions are declared in string.h but this is included in mbed.h.

17 Nov 2012

How is it possible to printf only a part of the string perhaps 10-20.

Thanks for reply.

17 Nov 2012

Afaik you can do that by doing a number in front of the 's', so for example %20s for 20 chars.

19 Nov 2012

Sorry for the late reply, I had to leave for work... But thanks for the reply's, great to have such a support!

19 Nov 2012

Sorry but that with the %20s dont works. And wath is if i want print from char 10 to char 15??

19 Nov 2012

Googled it, and indeed missed a dot. Correct one is %.20s. Regarding starting char, remember that our 'string', isnt more than a pointer to an array of chars. The pointer points to the first one, so if you dont want it to start there, you can let it point to somewhere halfway your array.

#include "mbed.h"

int main() {
    char foo[] = "0123456789ABCDEF";
    printf("Test run = %.3s\n", &foo[7]);
}

Here it starts at position 7 of the array (starting from 0), so its output is "789".

22 Nov 2012

Thanks now i have the problem that if the char isnt full 1. Field after the last sign is black on the lcd. Do you now how can i clear this.

char Notizschreiben[31];
   
 lcd.cls();
    lcd.locate(0,0);
    lcd.printf("%.16s\r\n", Notizschreiben);
    lcd.locate(0,1);
    lcd.printf("%.16s\r\n", &Notizschreiben[15]);
22 Nov 2012

Try it without the \r and \n.

22 Nov 2012

Perfect. Thanks You