Reputation: 163
I encountered a problem when trying to add substrings to a string in C:
I have a variable that holds data, for example: "0%, 35.6, 0:1,..."
and I'm trying to add titles to each parameter, for example: "title1: 0%, title2: 35.6, title3: 0:1,..."
.
I've tried several methods, including using strtok()
, but with no luck so far. In this case I suspect a casting problem (the variable that holds the data (Debug
in the code below) is an UINT8*
), but I don't seem to be able to solve it. Could someone please offer me some directions?
static void SendDebug(UINT8* Debug) {
char *TempToken;
char *array[13] = {"some", "titles", "for", "substrings", "here", "...", "...", "...", "...", "...", "...", "...", "..."};
int j=0;
int i=0;
sprintf((char *) Debug, "%s,","New_String:");
strcpy(TempToken, (char *)Debug);
for (i=0; i<13; i++) {
strcat((char *)Debug, array[i]);
while (TempToken[j] != ',' && TempToken[j] != '\0') {
strcat((char *)Debug, (char *)TempToken[j]);
j++;
}
j++;
}
}
Thanks a lot!
Upvotes: 1
Views: 73
Reputation: 409176
TempToken
is just an uninitialized pointer, using it in any way (except to initialize it) leads to undefined behavior.
Declare it either as a character array big enough, or dynamically allocate it.
Upvotes: 4