'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");
c


Solution 1:[1]

  1. You do not need to strcpy at all as the HAL function is blocking.
  2. int size = strlen(&arr); size should be size_t and you need to pass arr not &arr. &arr is giving the address of the local variable arr not the reference stored in this variable.
  3. buff is too short and it has to be one char longer 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___________