transcend
transcend

Reputation: 95

c function return array and compare array against another

The function below reads characters from a UART and puts them in an array. It contains a repsonse from a hardware device.

In main I want to check that the array contains the correct response.

How can I get getData() to return an array and how can I compare this array to the correctResponse array?

void getData(int length){
    int i;
    int buffresponse[6];
    for (i = 0; i < length; i++)
    {
        //Perform a single character read from the UART interfac
        buffresponse[i] = getcharacter(UART_4);
       }
       buffresponse[i] = 0;
}

int main(void)
{
unsigned char correctResponse[] = { 0x56, 0x00, 0x26, 0x00 };
getData();  
}
}

Upvotes: 1

Views: 72

Answers (4)

RastaJedi
RastaJedi

Reputation: 663

Well, first of all, you can never return an array. You have two options, the more commonly used option is to create the array in main, and pass a pointer to it as one of your arguments (generally "output" arguments are the last one, but this obviously doesn't matter), or your other option, is to create the array in your function (it has to be static otherwise it will go out of scope once the function returns), and return a pointer to the array. Considering that this 'static' method isn't recommended, your second option could instead be malloc()ing space for the array in your function, and returning the pointer to that (don't forget to free() in main!).

The video that @OleksandrKravchuk links to returns a pointer to a local variable, this is a bad idea (you can use the static trickery, but this is not a perfect solution either. 99% of time having it in as an argument is the way to do this). To be clear, there is NO WAY to return an array (by value, anyways). Don't let his answer confuse you.

Upvotes: 0

tdao
tdao

Reputation: 17688

How can I get getData() to return an array

There is actually no need for getData() to return an array. You can just pass the array to the function, and modify it directly.

So instead of void getData(int length), you can write your prototype as:

void getData( unsigned char response[], int length );   // no need local array `buffresponse`

how can I compare this array to the correctResponse array?

Given the two arrays correctResponse and response of the same type unsigned char and the same size, you can use memcmp to compare and see if reponse matches correctResponse. Example,

int ret_val = memcmp( correctResponse, response, sizeof(correctResponse) );
if( ret_val == 0 ) 
    printf( "Response match.\n" );

Upvotes: 0

unwind
unwind

Reputation: 400009

The only way to return an actual array is to wrap it in a struct and return that by value. You probably don't want to do that. The typical solution is to pass in the output buffer, like so:

void getData(unsigned char *buf, size_t length)
{
}

then the caller would supply a buffer:

unsigned char response[6];
getData(response, sizeof response);
const char correctResponse[] = "\x56\x00\x26\x00\x00\x00";
const bool equal = memcmp(response, correctResponse, sizeof response) == 0;

Note: I extended correctResponse to be 6 bytes for the comparison to make sense.

Upvotes: 2

Oleksandr Kravchuk
Oleksandr Kravchuk

Reputation: 6327

There are multiple ways to return an array. I would highly recommend you investigate all of them for good understanding.

How to Return an Array from a Function in C

Regarding comparing arrays, this basic question has been asked like a million times already.

C array comparison

Upvotes: 0

Related Questions