简体   繁体   中英

Passing pointer of char array to function

Below is my function which takes a char array of size 2, passes it to a function and the function should return the same 2 chars back (its a bit of a convoluted process as it's talking to a hardware device). The problem is when I'm passing the char (*in)[2] into the function; When I print the elements it only displays the data in[0] and not in[1]. Not sure why. Any help appreciated.

int function(struct device *d, char (*in)[2], char (*out)[2])
{

    printf("in: %c %c\n", *in[0], *in[1]); // TEST
    uint16_t data = (*in[0] << 8) + *in[1];

    send_request(d, IN, ECHO, data, 0, *out, 2);

    printf("%s\n", *out);

    return 0;
}

Thanks Bob

I'm assuming you are passing addresses of arrays to you function() .

The problem is that [] operator has a higher precedence than * operator.

The correct code would be.

printf("in: %c %c\n", (*in)[0], (*in)[1]);

First you dereference in to get the original array and then you get the element of that array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM