Search code examples
cpointersuart

when to use pointers in function arguments in c, UART function


I'm reading some example code for a uart on a microcontroller.

In the header file for the uart libary there is the following function definition.

Then in the main program the following code is called.

My question is why do they use a pointer as a function argument?

Alternatively, the UART write function declaration below does not use a pointer.

I'm just wondering why and when to use pointers.

Many thanks


Solution

  • In the main function, the BANNER is cast as a uint8_t* because it is a string literal and as such has type const char[] that decays as const char* when used in a expression or passed as a function. char and uint8_t are different types, although their representation is identical and only the interpretation of certain values differ.

    There is another problem in the main function: you should pass strlen(BANNER) instead of sizeof(BANNER) so as not to include the null terminator in the count of bytes to transmit.

    Here is a corrected version:

    #define BANNER  "Hello, world!\n\n"
    
    int main(void) {
        ti_uart_write_buffer(TI_UART_0, (const uint8_t *)BANNER, strlen(BANNER));
        return 0;
    }