Reputation: 197
I am using strcat to build a string. It works except for when I want to append characters that represent numbers from an array. The line that uses:
strcat(JsonDataStr, numsToSend[i]);
for example appends the character 'c' instead of '1'. If I manually put the character in using double quotes it works, but i want to have a one dimensional array with characters only.
char JsonDataStr[20];
void buildJsonString(){
int offset;
char strtStr[] = "[{\"" ;
char numStr[4];
char numsToSend[4] = {'1', '2','3','4'};
offset = sizeof(strtStr);
strcat(JsonDataStr, strtStr);
for(i = 0 ; i < 2 ; i++){
strcat(JsonDataStr, JsonDataName);
ByteToStr(i, numStr);
strcat(JsonDataStr, numsToSend[i]);
strcat(JsonDataStr, "\":\"");
}
strcat(JsonDataStr, "\"}]");
}
Any idea why this is happening.
Upvotes: 0
Views: 151
Reputation: 16876
You can't use strcat
like this. It appends strings to strings, not single chars to strings. A quick fix would be to have numsToSend
as an array of char pointers instead:
char *numsToSend[4] = { "1", "2", "3", "4" };
Other than that you could write a function that appends chars to strings, see this answer for an example.
Upvotes: 3