'How can I fetch the length of a character array and put it in an array?
I'm working with my STM32 but since I'm new to c/c++ (coming from c#) i find string operations a bit difficult.
Until now I'm sending serial like this:
strcpy((char*)buf, "Unable set read MCP9808\r\n");
HAL_UART_Transmit(&huart1, buf, strlen((char*)buf), HAL_MAX_DELAY);
But I want to change that to a onliner using Debug function that I made. But im getting error 'strlen' from incompatible pointer type. I want to take inn any string (char array), get its length to set up the buffer and send the data.
int Debug(char arr[])
{
int size = strlen(&arr);
uint8_t buf[size];
strcpy((char*)buf, arr);
// the above code is the important part...
HAL_UART_Transmit(&huart1, buf, strlen((char*)buf), HAL_MAX_DELAY);
return 1;
}
Debug("Hello World!\r\n");
Solution 1:[1]
- You do not need to
strcpyat all as the HAL function is blocking. int size = strlen(&arr);sizeshould besize_tand you need to passarrnot&arr.&arris giving the address of the local variablearrnot the reference stored in this variable.buffis too short and it has to be onecharlonger to store terminating null character.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 0___________ |
