Due Programming different than other boards

I have a sketch that I’m working on where I define different behavior based on whatever board is currently selected (using the pins_arduino.h file). However, I’m noticing different behavior when I use the same code I would use normally but have the Due board selected. For example, let’s take a look at this code:

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)  // declare variables for MEGA boards
  int MEGA_READ_DPIN_CONFIG[] = {22,23,24,25,26,27,28,29,30,31}; 

#if defined(__SAM3X8E__)  // declare variables for DUE boards
  int DUE_READ_DPIN_CONFIG[] = {22,23,24,25,26,27,28,29,30,31}; 

The code is identical (with different array names). However, later in my code I have these two loops:

  #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)  //set pin mode for MEGA boards
    for(int i = 0; i < MEGA_READ_DPIN_CONFIG.length; i++){
  #if defined(__SAM3X8E__)  //set pin mode for DUE boards
    for(int i = 0; i < DUE_READ_DPIN_CONFIG.length; i++){
      pinMode(DUE_READ_DPIN_CONFIG[i], INPUT);

However, this is where I run into an error when I try to compile the sketch (see below)

Firefly_Firmata_DUE.ino: In function 'void Init()':
Firefly_Firmata_DUE.ino:132:45: error: request for member 'length' in 'DUE_READ_DPIN_CONFIG', which is of non-class type 'int [10]'
Error compiling.

If I comment out the if def function for the DUE board, everything compiles just fine. So, it seems like the compiler is getting hung up on the length of the array that I created for the DUE board. Does anyone know why this is happening? or how to fix it?

DUE_READ_DPIN_CONFIG is an array, it doesn't have member functions. Either make a #define for the length or use sizeof()/sizeof(array_name[0])

Also why not make the array name the same so you don't need #if in the main code?


Thanks for the reply. Quick question though. You mentioned that DUE_READ_DPIN_CONFIG is an array and doesn't have member functions... however, MEGA_READ_DPIN_CONFIG is also an array and I can call the .length member function on it, and it seems to work. Why would it work on the MEGA array, but not the DUE?
Secondly, I'd rather not use a define if necessary to specify the length so I'm trying your other method... but having trouble. I tried this:

int len = sizeof()/sizeof(DUE_READ_DPIN_CONFIG[0]);

But this gives me an error stating that it expected a primary-expression before the ')' token. I assume I'm writing it wrong. Lastly, good advice on making the names the same. I'll do that.

My bad, try

int len = sizeof(DUE_READ_DPIN_CONFIG)/sizeof(DUE_READ_DPIN_CONFIG[0]);

I don’t know why .length works on one array and not the other. I’m not into the new(er) versions of C++ being a straight C person in the main. Maybe more recent versions do allow that, and when you are compiling for the Mega there are different options in play behind the scenes.


Yep. That seems to work well. Thanks!

Mega uses avr-gcc, Due uses a different compiler to generate ARM code. Not
the same compiler...