user17724634
user17724634

Reputation:

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

Upvotes: 0

Views: 989

Answers (1)

0___________
0___________

Reputation: 67476

  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.

Upvotes: 1

Related Questions