1

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

1
  • This: *in[0] likely isn't parsing how you think it is. Likewise for *in[1]. Commented Oct 2, 2014 at 6:57

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You posted first so I suppose its only fair to give you the points. And thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.