Bob Jane
Bob Jane

Reputation: 217

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

Upvotes: 1

Views: 181

Answers (1)

2501
2501

Reputation: 25753

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.

Upvotes: 4

Related Questions