Pointer Comparison Fails

The code execution fail at instruction while ( ptr1 != ptr2) {…}, after updating pointers ptr1 and ptr2 on interrupts and declaring the following:
extern volatile unsigned char *ptr1;
extern volatile unsigned char *ptr2;

Why does this happen?

The problem can be addressed by changing the declaration to:

extern unsigned char * volatile ptr1;
extern unsigned char * volatile ptr2;

Using this declaration, the pointer is qualified as volatile instead of the object.

For more information, please read the chapter "8.7.1 Combining Type Qualifiers and Pointers" of the "MPLAB® XC16 C Compiler User’s Guide".

Any variable used in an ISR and the main code must be qualified as volatile because the compiler would not know when the variable is modified in the ISR.

© 2018 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.