0

I have a system where an RFID tag can be checked and Its individual 4byte UID will be printed. My task is to check if subsequent tags have the same UID and if not, print “access denied”. I'm assuming i need to use const char but I'm not sure. I get an error at if (myrfid.serNum[i] == ID) saying comparison between pointers and integers are forbidden. Any sort of clarity would be appreciated, thanks!

(Code is only a section from the for loop)

void loop()
{
    int i;
    const char ID[] = "111 11 11 11";

    cardpresent = myrfid.isCard();

    if (cardpresent && ! lastcardpresent)
    {
        if (myrfid.readCardSerial())
        {
            for (i = 0; i < 4; i++)
            {
                if (myrfid.serNum[i] == ID)
                {
                    Serial.print("Acess granted ");
                }
                else
                {
                    Serial.print("Acess not granted ");
                }
            }
        }
        Serial.printf("%3d ", myrfid.serNum[i]);
    }
    Serial.print("\n");

    // ...
}

1 Answer 1

1

The accepted way to initialize an array in C follows this example:

const char ID [] = {111, 11, 11, 11};

You can read more about it here.

You later use an array name ("ID") with no index ("ID[0]"). The C compiler assumes the programmer who uses an array name with no index is interested in the memory address of the first element of the array. But you wanted the value in that memory location. So you should use the array name with the appropriate index. Guessing, it would appear you intended to use "ID[i]" in the above code fragment.

2
  • Ah, i see. Thanks for the explanation, fixed the major issues i had!
    – Zèro
    Mar 8, 2020 at 20:30
  • Good. If you understood that - welcome to C Programming with pointers. Pointers are very useful. And, honestly, you have a leg up on a lot of C programming beginners if you understand the difference between the address of a variable verses the value of the variable.
    – st2000
    Mar 8, 2020 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.