Code compiles for one MPU but not for another: invalid conversion..[solved].

Hello,

Consider the following :

long TotalYield = bytes2long(*regs);


long bytes2long(byte *bytes_array){
  // Create union of shared space
  long val;
  union {
    long long_variable;
    byte temp_array[4];
  } u;
  // Overite bytes of union with bytes variable
   
  // Assign bytes to input array
//  memcpy(bytes_array, u.temp_array, 4);
  memcpy(u.temp_array, bytes_array, 4);
  val=u.long_variable;
  return (val);
}

The above compiles fine for arduino micro, leonardo and Mega

For some reason when the ATMEga1284p MightyCore board is selected the following messages is returned:

invalid conversion from 'int' to 'byte* {aka unsigned char*}' [-fpermissive]
pointing to this line :
long TotalYield = bytes2long(*regs);

Is this MPU related ?

Please provide a complete, minimal demonstration sketch.

pert:
Please provide a complete, minimal demonstration sketch.

Heres one:

byte regs[]={1,2,3,4} ;    //Random values for demo


void setup() {
  
long TotalYield = bytes2long(*regs);
Serial.println(TotalYield);

}

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

}




long bytes2long(byte *bytes_array){
  // Create union of shared space
  long val;
  union {
    long long_variable;
    byte temp_array[4];
  } u;
  // Overite bytes of union with bytes variable
   
  // Assign bytes to input array
//  memcpy(bytes_array, u.temp_array, 4);
  memcpy(u.temp_array, bytes_array, 4);
  val=u.long_variable;
  return (val);
}

If you have warnings turned on (File > Preferences > Compiler warnings). You will see that a warning similar to the compiler error is produced when compiling for Mega, Uno, Leonardo:

E:\electronics\arduino\temp\sketch_jan20b\sketch_jan20b.ino: In function 'void setup()':

E:\electronics\arduino\temp\sketch_jan20b\sketch_jan20b.ino:7:35: warning: invalid conversion from 'byte {aka unsigned char}' to 'byte* {aka unsigned char*}' [-fpermissive]

 long TotalYield = bytes2long(*regs);

                                   ^

E:\electronics\arduino\temp\sketch_jan20b\sketch_jan20b.ino:20:6: note: initializing argument 1 of 'long int bytes2long(byte*)'

 long bytes2long(byte *bytes_array){

      ^

So why is it a warning for Arduino AVR Boards but an error for MightyCore boards? The reason is that Arduino AVR Boards has the -fpermissive flag in its compile recipes and MightyCore does not.

This flag was added in Arduino AVR Boards 1.6.12 (which is shipped with Arduino IDE 1.6.10):

The reason is that the new compiler version updated to in that version caused some things that had previously been warnings to be upgraded to errors. So for backwards compatibility -fpermissive was added. However, that also cause some things that used to be errors to be downgraded to warnings. The code you posted is one of these. If you use Boards Manager to change your Arduino AVR Boards version to 1.6.11 or older you will find that the code that used to compile for Mega, Uno, Leonardo now produces the same compilation error as you encounter with MightyCore. Therefore that code is not backwards compatible with old versions of Arduino AVR Boards (and therefore old versions of the Arduino IDE).

Note what was written in the commit adding -fpermissive:

It may possibly be removed in
the future after a period of transition.

So that code may compile for recent versions of Arduino AVR Boards but it's not guaranteed to do so in the future.

So moral of the story is to always have warnings set to "All", always pay attention to warnings, and always fix warnings.

Thanks for the explanation in the difference of behavior between the compilation for different boards boards.
I do have the compiler warnings enabled by the way but didnt know what -fpermissive is.. now i do!

It may now sound trivial, but still, why exactly is it complaining about this code ?

The compiler is telling you that bytes2long takes a pointer to some bytes as its argument, but you passed it *regs which is a byte. Try passing regs instead.

wildbill:
The compiler is telling you that bytes2long takes a pointer to some bytes as its argument, but you passed it *regs which is a byte. Try passing regs instead.

Thank you.