How can I find the length of the string from address of variable(&variable) ? Below is the code :
SimpleProfile_GetParameter(SIMPLEPROFILE_CHAR7, &newValue); // Hello123
const char echoPrompt[] = "Print From BLE characters:\r\n";
UART_write(uart, echoPrompt, sizeof(echoPrompt)); // Output : Print From BLE characters: | Size : 29
UART_write(uart, &newValue, sizeof(&newValue)); // Output : Hello | Size : 4
I am using this code in Code Composer Studio (CCS). I need to print the string in UART, Where I need to specify number of character in the string.
I need "Hello123" to be printed instead its printing "Hello"
&newValue
is a pointer, sosizeof(&newValue)
returns the size of a pointer, not the string it points to. AssumingnewValue
is a null-terminated string, usestrlen()
.sizeof
operates at compile time, it can't get the size of a string that's constructed dynamically.You should also do that with
echoPrompt
, becausesizeof
includes the trailing null byte, and you probably don't need to write that.