V
V
Vlad Zaitsev2017-01-08 02:44:40
C++ / C#
Vlad Zaitsev, 2017-01-08 02:44:40

How to pass a pointer to an array into a function?

I have this C code:

static uint8_t uart_command_buf[42];
...
void uart_packet_damp(uint8_t uart_command_buf[]) {
    for(int i = 0; i <= 41; i++)
    {
        printf("0x%02X ", uart_command_buf[i]);
    }
}
...
uart_packet_dump(uart_command_buf);

How can I rewrite it to pass a pointer to an array instead of a copy of the array? It doesn’t work on the forehead (more precisely, it’s going to, but swears):
static uint8_t uart_command_buf[42];
...
void uart_packet_dump(uint8_t *uart_command_buf[]) {
    for(int i = 0; i <= 41; i++)
    {
        printf("0x%02X ", *uart_command_buf[i]);
    }
}
...
uart_packet_dump(&uart_command_buf);

ud-rpl_root.c: In function 'char_in':
ud-rpl_root.c:151:26: warning: passing argument 1 of 'uart_packet_dump' from incompatible pointer type [-Wincompatible-pointer-types]
         uart_packet_dump(&uart_command_buf);
                          ^
ud-rpl_root.c:117:1: note: expected 'uint8_t ** {aka unsigned char **}' but argument is of type 'uint8_t (*)[42] {aka unsigned char (*)[42]}'
 uart_packet_dump(uint8_t *uart_command_buf[]) {
 ^~~~~~~~~~~~~~~~

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wexter, 2017-01-08
@vvzvlad

Should be void uart_packet_dump(uint8_t *uart_command_buf)
uint8_t *uart_command_buf[] similar to uint8_t **uart_command_buf

M
Maxim Moseychuk, 2017-01-08
@fshp

In C, you cannot pass an array by value.
This is where passing by pointer happens.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question