-1

Probably a very simple question but I couldn't figure out the reason. All the given code is in the same file.

This is the definition of one of the arrays. It is defined outside of every method.

unsigned char KEY_40_COMPARE_VALUES[] = {
85,102
,119,134
,147,158
,165,169
,169,165
,158,147
,134,119
,102,85
,67,50
,35,22
,11,4
,0,0
,4,11
,22,35
,50,67
};

This is the code:

unsigned char * getCompareValuesForIndex(char index)
{
    if (index == 0)
    {
        return KEY_28_COMPARE_VALUES;
    }

    if (index == 1)
    {
        return KEY_30_COMPARE_VALUES;
    }

    if (index == 2)
    {
        return KEY_32_COMPARE_VALUES;
    }

    if (index == 3)
    {
        return KEY_33_COMPARE_VALUES;
    }

    if (index == 4)
    {
        return KEY_35_COMPARE_VALUES;
    }

    if (index == 5)
    {
        return KEY_37_COMPARE_VALUES;
    }

    if (index == 6)
    {
        return KEY_39_COMPARE_VALUES;
    }

    else
    {
        return KEY_40_COMPARE_VALUES;
    }
}

This is the compilation error I get:

conflicting types for 'getCompareValuesForIndex'
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Utku
  • 2,025
  • 22
  • 42

1 Answers1

3

An error like this usually happens when you make a call of your function before providing a prototype for it, and ahead of its declaration in the file. In this case the compiler assumes that

  • Any arguments the function takes are of type int, and
  • The value the function returns is of type int

Essentially, the function looks like this to the compiler:

int getCompareValuesForIndex(int index) { // <<= What compiler thinks
    ...
}

When compiler sees the actual declaration, it issues an error.

To fix this problem, either (1) provide a function prototype ahead of the call, or (2) move the definition to be ahead of the first use of your function.

Note: With this issue out of the way, consider optimizing your function, by adding a static array, like this:

unsigned char *KEY_COMPARE_VALUES[] = {
    KEY_28_COMPARE_VALUES
,   KEY_30_COMPARE_VALUES
,   KEY_32_COMPARE_VALUES
,   KEY_33_COMPARE_VALUES
,   KEY_35_COMPARE_VALUES
,   KEY_37_COMPARE_VALUES
,   KEY_39_COMPARE_VALUES
};

Now your function can be rewritten like this:

unsigned char * getCompareValuesForIndex(char index) {
    return (index >=0 && index < 7) ? KEY_COMPARE_VALUES[index] : KEY_40_COMPARE_VALUES;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523