简体   繁体   English

无法在C函数中返回'true'值

[英]unable to return 'true' value in C function

If Im trying to check an input 5 byte array (p) against a 5 byte array stored in flash (data), using the following function (e2CheckPINoverride), to simply return either a true or false value. 如果我试图检查输入5字节数组(p)对存储在闪存(数据)中的5字节数组,使用以下函数(e2CheckPINoverride),只返回true或false值。 But it seems, no matter what I try, it only returns as 'false'. 但似乎无论我尝试什么,它只会返回'假'。

I call the function here: 我在这里调用函数:

if (e2CheckPINoverride(pinEntry) == 1){
  PTDD_PTDD1 = 1; 
}
else{
  PTDD_PTDD1 = 0; 
}

Here is the function: 这是功能:

BYTE e2CheckPINoverride(BYTE *p)
{
    BYTE i;
    BYTE data[5];

if(e2Read(E2_ENABLECODE, data, 5)) {
    if(data[0] != p[0]) return FALSE;
    if(data[1] != p[1]) return FALSE;
    if(data[2] != p[2]) return FALSE;
    if(data[3] != p[3]) return FALSE;
    if(data[4] != p[4]) return FALSE;
}
return TRUE;
}

I have already assigned true and false in the defines.h file: 我已经在definitions.h文件中分配了true和false:

#ifndef TRUE
    #define TRUE ((UCHAR)1)
#endif

#ifndef FALSE
    #define FALSE ((UCHAR)0)
#endif

and where 在哪里

typedef unsigned char   UCHAR;

when i step through the code, it performs all the checks correctly, it passes in the correct value, compares it correctly and then breaks at the correct point, but is unable to process the return value of true? 当我单步执行代码时,它会正确执行所有检查,传递正确的值,正确比较然后在正确的点处中断,但是无法处理true的返回值?

please help? 请帮忙?

#define TRUE 1
#define FALSE 0

Forget the unsigned char. 忘记unsigned char。 You can go with the premise that in c 0 is false everything else is true 你可以假设在c 0是假的,其他一切都是真的

Probably not going to solve your problem, but you should write: 可能不会解决你的问题,但你应该写:

PTDD_PTDD1 = e2CheckPINoverride(pinEntry) ? 1 : 0;

Also, you are mixing BYTEs and UCHARs (even though they are probably the same) 此外,你正在混合BYTE和UCHAR(即使它们可能是相同的)

Try narrowing this down by dispensing with the #define and just saying 尝试通过免除#define然后说出来缩小范围

return 1;

If that works, then something isn't working with your #define's. 如果这样可行,那么某些东西不适用于你的#define。

If you return TRUE or FALSE , you should also check for them. 如果您返回TRUEFALSE ,您还应该检查它们。 Rewrite the if clause like this: 像这样重写if子句:

   if (e2CheckPINoverride(pinEntry) == TRUE) { // instead of '== 1'

IMO, you're creating a great deal of unnecessary complexity. IMO,你正在创造大量不必要的复杂性。 I'd write the function something like this: 我写的函数是这样的:

int e2CheckPINoverride(BYTE *p) {
    BYTE data[5];

    return e2Read(E2_ENABLECODE, data, 5) && 
        data[0] == p[0] &&
        data[1] == p[1] &&
        data[2] == p[2] &&
        data[3] == p[3] &&
        data[4] == p[4];
}

And the calling code becomes simply: 调用代码变得简单:

PTDD_PTDD1 = e2CheckPINoverride(pinEntry);

Just a small example that I tested to work. 只是我测试工作的一个小例子。 I dont know the content of e2Read so I made just a dummy 我不知道e2Read的内容,所以我只做了一个假人

#ifndef UCHAR
    typedef unsigned char   UCHAR;
#endif
#ifndef BYTE
    typedef  char   BYTE;
#endif
#ifndef TRUE
    #define TRUE ((UCHAR)1)
#endif
#ifndef FALSE
    #define FALSE ((UCHAR)0)
#endif
int e2Read(int myconst, BYTE* data, int num)
{
    int i;
    for(i=0;i<num;i++)
        *(data++) = 0; // You can change thisone to test different results.
    return 1;
}
BYTE e2CheckPINoverride(BYTE *p)
{
#define E2_ENABLECODE 3
    BYTE data[5];
    if(e2Read(E2_ENABLECODE, data, 5)) {
    if(data[0] != p[0]) return FALSE;
    if(data[1] != p[1]) return FALSE;
    if(data[2] != p[2]) return FALSE;
    if(data[3] != p[3]) return FALSE;
    if(data[4] != p[4]) return FALSE;
    }
    return TRUE;
}
int main(void)
{
    BYTE b[5] = {0,0,0,0,0};// You can change thisone to test different results.
    BYTE* pinEntry = b;
    if (e2CheckPINoverride(pinEntry) == 1){
        printf("Returned true\n");
    }
    else{
        printf("Returned false\n");
    }
    return 0;
}

Your data array is never initialised so it has random values inside. 您的数据数组从未初始化,因此内部具有随机值。

BYTE data[5];

So, you are comparing the elements from array p with random values from array data. 因此,您要将数组p中的元素与数组数据中的随机值进行比较。 It will return almost always FALSE. 它几乎总是返回FALSE。

Conclusion: Fill the data array with meaningful data. 结论:使用有意义的数据填充数据数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM