0

I have the following code snippet. I'm trying to create a function that compares that values of array and array2. The function com_read(); loads values array2 and I'm not allowed to modify this function. So I need to compare the two arrays after calling com_read(); but I'm not sure how to do it since I'm not that familiar with structures in C. I would appreciate any suggestions. Thanks.

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>

#define ARRAY_LENGTH(x) (sizeof(x) / sizeof((x)[0]))

uint8_t array[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
uint8_t array2[5];

uint16_t sineLookupTable[] = {128, 218, 255, 218, 128, 37, 0, 37 };

#define test_failure 0;
#define test_success 1;

typedef struct
{
    int dr_assert;
    int dr_fail;
} dr_result_t;

typedef struct 
{
    uint8_t* Firstbuff;
    uint16_t Firstbuff_size;
    uint8_t *Secondbuff;
    uint16_t Secondbuff_size;
    dr_result_t result;

} dr_config_data;

struct i2c_open
{
    dr_config_data initconfig;
    dr_result_t expectedResult;
};

struct i2c_open readDATA[1] =
{
    {{array, ARRAY_LENGTH(array),array2, ARRAY_LENGTH(array2)}, 0x01}
};

int com_read(uint8_t id, dr_config_data* data) {
    int status = test_failure;

    //do stuff
    status = test_success;
}


int main() {
    // printf() displays the string inside quotation

    int result = 0;
    dr_result_t result2;

    result = com_read(0, &readDATA[0].initconfig);
    result2 = readDATA[0].expectedResult;
    printf("result2 = %d\n", result2);
    return 0;
}
pekoms
  • 55
  • 6
  • 1
    Does this answer your question? [Best way to compare two int arrays of the same length?](https://stackoverflow.com/questions/9120065/best-way-to-compare-two-int-arrays-of-the-same-length) – nielsen Jun 09 '23 at 06:07
  • yes. thank you . – pekoms Jun 09 '23 at 17:41

2 Answers2

2

... create a function that compares that values of array and array2.

Since the 2 arrays:

  • Lack any padding in the elements.
  • Are simple integers.
  • The same size.
  • The same type.

... use memcmp().

array[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
uint8_t array2[5];

bool equal = memcmp(array, array2, sizeof array) == 0;

If the above conditions are not met, code may need a different approach.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

I do not exactly understand the structure of the code and your question. But since array and array2 are global variables, you can compare their content simply by accesing their elements using array[i] and array2[i], where i is the index.

If you would like to access them through the struct, you can access struct elements using the dot notataion like data->Firstbuff[i], where data is the pointer to the struct. The arrow -> is used instead of the ., because data is a pointer. You could also use (*data).Firstbuff[i] to first derefence the pointer and then access struct's field.

Another problem I currently see with this code is that values of array2 are never set, which means all its elements are equals to 0.

Blaz
  • 123
  • 6