开发者

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. 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:

#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?

please help?


#define TRUE 1
#define FALSE 0

Forget the unsigned char. You can go with the premise that in c 0 is false everything else is true


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)


Try narrowing this down by dispensing with the #define and just saying

return 1;

If that works, then something isn't working with your #define's.


If you return TRUE or FALSE, you should also check for them. Rewrite the if clause like this:

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


IMO, you're creating a great deal of unnecessary complexity. 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

#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. It will return almost always FALSE.

Conclusion: Fill the data array with meaningful data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜