Purgoufr
Purgoufr

Reputation: 972

One-dimensional array comparison within a two-dimensional array in C

I want to compare two arrays of different sizes in a particular function. I wrote a function like this:

static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][])
{
    static int index=0;
    static bool isequal = false;

    for(int i = 0 ; i<count_current; i++ )
    {
        for(int j = 0; j<6 ; j++ )
        {
            if (array_1[j] == array_2[i][j])            
            {
                index++ ;
            }
            else
            {
                index=0;
                return false;
            }
        }
        if (index == 6)
        {
            index=0;
            isequal = true;
            i = count_current + 2;
        }
        else
        {
            index=0;
            isequal = false; 
        }   
    }
    return isequal;
}

Define some variables;

static bool matching_array;
static uint8_t one_dimensional_array[6];
static uint8_t two_dimensional_array[10][6];    

Then I use the function like this;

matching_array= compare_arrays( one_dimensional_array, two_dimensional_array);

But I got an error like this;

..\..\..\main.c(857): error:  #98: an array may not have elements of this type

Do you have any suggestion ?

Upvotes: 0

Views: 797

Answers (1)

user2736738
user2736738

Reputation: 30926

static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][])

The second dimension must be present - otherwise compiler can't decide how much position to stride through when someone writes like array_2[y][x].

static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][6])

Actually 2d array decays into pointer to it's first element - which is of type uint8_t (*)[6]. And the single dim array will similarly decay here into uint8_t*. Remember that the first dimension of passed array is not considered by compiler.Arrays declarations must have all, except the first, sizes defined.

Upvotes: 2

Related Questions