I am working with existing code and am trying to not butcher the code that outputs text on a touchscreen display. The text is defined as int8_t
and will not allow me to combine text with integer. I am doing this on a TI launchpad MSP432 with booster pack K350QVG. I have done multiple searches on this site and Google but can't get code to work that others have suggested and would like some help and explanation please.
Some code I am working with:
Graphics_drawStringCentered(&g_sContext, "Draw Rectangles",
AUTO_STRING_LENGTH, 159, 15, TRANSPARENT_TEXT);
The "Draw Rectangles" I would like to change to "Value equals: " + Value
void Graphics_drawStringCentered(const Graphics_Context *context,
int8_t *string, int32_t length, int32_t x, int32_t y,
bool opaque)
{
Graphics_drawString(context, string, length,
(x) - (Graphics_getStringWidth(context, string, length) / 2),
(y) - (context->font->baseline / 2), opaque);
}
When I try to add it, I get this error
#
169-D argument of type " char *
" is incompatible with parameter of type " int8_t *
"* I have tried several methods of converting an int
to int8_t
but have not found anything that works. Can you please help suggest what to try and I will post my results.
It sounds like you are trying to concatenate strings using the "+" operator. You cannot use the "+" operator to concatenate strings in C. Instead, you have to allocate the memory for the new string yourself, and then you can use the standard library function strncat() from string.h to concatenate the strings.
The second issue is the use of int8_t* instead of char* for a C string. That is not a standard type for strings in C, and I don't know why the existing code uses it. However, if you are just using ASCII characters, then casting when calling Graphics_drawStringCentered() should work.
#include <string.h>
int8_t* Value = (int8_t*)"123"; /* string using a strange type */
char theString[256]; /* create a 256-byte buffer to hold the string */
strncpy(theString, "Value: ", 256); /* initialize the buffer with the first string */
strncat(theString, (char*)Value, 256); /* append the second string */
theString[255] = '\0'; /* ensure the string is NULL-terminated */
Graphics_drawStringCentered(&g_sContext, (int8_t*)theString,
AUTO_STRING_LENGTH, 159, 15, TRANSPARENT_TEXT);
Graphics_drawString()
does not need the string buffer to persist after it returns. strncpy()
and strncat()
. If your logic allows you to build the whole string at once, you can use snprintf(theString, 256, "Value: %s", (char*)Value);
as a simpler alternative. (Unlike strncpy()
and strncat()
, snprintf()
will always NULL-terminate the string.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.