Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 8 months ago.
String merging problems
Import programTest_doAddFollowingChars_Function
Formatting strings with appending chars to use for LCDs or tab-like formatting
This function is acting weird. if the test string is long enough it adds the correct amounts of appending chars, eg. when the test variable is 12345. If the variable is shorter, like 12, the script crashes.
Someone knows why? Or maybe there is a better way to do this?
Thanks in advance, Michael
the piece of code that (sometimes>) work
#include "mbed.h" DigitalOut myled(LED1); // String auf Länge mit beliebigem Zeichen auffüllen >-------------------------------------------------------------------- void doAddFollowingChars(char* stringMessage, int intLange, char* stringFullZeichen) { int intStringLange = 0; intStringLange = strlen(stringMessage); char stringSpaceString[] = ""; printf("%d - %d\n", intLange, intStringLange); if (intStringLange >= intLange) { printf("The string can not be longer.\n"); stringMessage = stringMessage; } else { for (int i = intStringLange; i < intLange; i++) { printf("This should be %d filling block(s): ", (i - intStringLange + 1)); strcat(stringSpaceString, stringFullZeichen); printf("%s\n", stringSpaceString); } strcat(stringMessage, stringSpaceString); } printf("The final string is: %s\n", stringMessage); } // String auf Länge mit beliebigem Zeichen auffüllen <-------------------------------------------------------------------- int main() { wait(0.2); char test[] = "123456"; int intStrLen = strlen(test); myled = 1; wait(1); myled = 0; wait(1); doAddFollowingChars(test,10,"a"); while(1) { myled = 1; wait(1); myled = 0; wait(1); } }
2 Answers
9 years, 8 months ago.
You need to use strings correctly. Check here http://www.tutorialspoint.com/cplusplus/cpp_strings.htm and take note of the use of \0 .
9 years, 8 months ago.
char test[] = "123456"; reserves a storage for 7 bytes (6 char + \0)
and your call "doAddFollowingChars(test,10,"a"); " give a different length of 10.... why did you compute intStrLen without using it?
You go to a crash !
I added the arrays dimensions and now it works. its not very comfortable since the array lenght is set to a value that could be reached still.
posted by 14 Apr 2015Yes! For a complete answer go here http://www.cplusplus.com/reference/cstring/strlen/ strlen is useful for variable length strings.
posted by 14 Apr 2015Quote:
I added the arrays dimensions and now it works. its not very comfortable since the array length is set to a value that could be reached still.
Which is why most c string functions have a version which in addition to a pointer to the string also take and an integer giving the maximum size of the buffer. e.g. sprintf makes it possible to overflow the buffer, snprintf doesn't. Either be very careful with your code, take the buffer size as a parameter and cut the end off the string if it's too long or have your function use malloc to allocate a new buffer that is large enough for the final result but if you do that be very careful of memory leaks.
And remember to always allow space for and add the '\0' on the end, without it other string functions will fail since they won't know when the string ends.
posted by 14 Apr 2015