Can't use a global called index ???


Has anyone else seen this problem

int index = 0;   

void setup() {
  // put your setup code here, to run once:


void loop() {
  // put your main code here, to run repeatedly:


Won't compile on Due

It compiles fine on Uno etc

Actually, I came across this issue while compiling for STM32, but it also happens on the Due as they are both ARM devices

Is this a known issue ??

There's a lot of macros and functions defined by the framework that supports your sketch. There are a lot of words you can't use for variables, not just the normal C reserved words.

Can you not use the time-honored "i" for your index?

My question is really, why does this not work on the Due when it works on the Uno.

I can easily change index to i, in fact I've already changed it to index1, as its not in code that I wrote and i is probably already used as well.

The name index can be declared as a local inside a function, so the use of the name index is not actually prohibited or reserved at all, it just doesn't compile when used as a global, and hence appears to be an issue with the IDE / compiler system for the Due.

My question is really, why does this not work on the Due when it works on the Uno.

Because they are different processors. The relative difference between a Due and Uno is actually greater than the difference between a PC and a Mac.

If you really want to know, search through Arduino\hardware\arduino\sam\ on your hard drive and be amazed at the amount of stuff that makes your sketch work.

Yes, the ARM chip has CMSIS and libsam libraries from the manufacturers to support all
the many thousands of registers and functions of the chip. Think of the Due as 50 times more
complex than the Uno and you'll get an idea of the difference...

Why you didn't post an error? It shows, conflicting declaration:

/arduino-1.5.8/hardware/tools/gcc-arm-none-eabi-4.8.3-2014q1/arm-none-eabi/include/string.h:55:8: error: previous declaration of 'char* index(const char*, int)'
char *_EXFUN(index,(const char *, int));
Error compiling.

The bottom line here is that string.h in AVR-GCC does not define index() whereas string.h in the ARM-GCC package does define index().

index() is a "BSD compatibility" function.

Because Arduino.h includes string.h and other standard includes, (presumably to save the user doing so) it means the user has to avoid those standard definitions. There is no way for the user to undefine them.

But if string.h was removed from Arduino.h I guess a lot of sketches would no longer compile.

It seems that a discrepancy between AVR and ARM is unavoidable here.