Chat freely about anything...

User avatar
By LetsCallItAFeature
#82907 Didn't know if this is more of a newbie or advanced question so I'm posting this here.
Part of a program I wrote for the esp8266 in the Arduino IDE is a function that "translates" a boolean array into an int array and returns the latter. The returned array is then given to a different function as an argument.

translation function:
Code: Select allint translate(bool data[], int nr, bool dir) {
  int real_data[48];
  real_data[47] = 1000;
  if (dir == false) {
    data[1] = true;
    if (nr == 1) {
      data[21] = true;
      data[23] = false;
    } else if (nr == 2 || nr == 3) {
      data[21] = false;
      data[23] = true;
    } else if (nr == 5 || nr == 6) {
      data[21] = false;
      data[23] = false;
    } else {
      data[21] = true;
      data[23] = true;
    }
  }
  for (int i = 0; i < 24; i++) {
    if (data[i] == true) {
      real_data[i * 2] = 1300;
      if (i < 23) {
        real_data[(i * 2) + 1] = 400;
      }
    } else {
      real_data[i * 2] = 400;
      if (i < 23) {
        real_data[(i * 2) + 1] = 1300;
      }
    }
  }
  return real_data;
}


function that uses the translated array:
Code: Select allvoid sendIr(int data[]) {
  digitalWrite(LED_BUILTIN, HIGH);
  //irsend.sendRaw(data ,48,36);        not important for now, doesn't change the result
  digitalWrite(LED_BUILTIN, LOW);
}


line that calls the functions (there are multiple but they are all like this)("r3" is a boolean array):
Code: Select allsendIr(translate(r3, 5, false));


When compiling I get the following error:
Code: Select allinvalid conversion from 'int' to 'int*' [-fpermissive]

for the lines
Code: Select allreturn real_data;
,
Code: Select allvoid sendIr(int data[]) {

and the lines that call the functions.
I am very confused why I get this error mainly because I never use pointers.
But it gets even weirder because when I select an Arduino board (uno, nano, ...) in the IDE and try to compile it I get no error. Do I need a library or anything else to make this work on the esp8266? All the examples for the esp8266 I tried out worked perfectly so I'm pretty sure I installed the esp stuff correctly.

Any ideas on what the issue could be and how to fix it? Thanks
User avatar
By quackmore
#83002 mmhh ...

the compiler is just telling you:

1) real_data type is "int *" cause you defined it as "int real_data[48]"
2) and you are trying to return it as a "int" (the function "translate" return an integer according to the definition)

the compiler is not telling you that what you are doing is really a bad idea

you are allocating an array on the stack (inside the function) and you are trying to return it for further use

unfortunately once the function is completed the array is gone (stack variables only exist while the function is running)

you better revise your code
for example
imagine your translate function working as strcpy
taking a source arrray and a destination array as parameters (passed by reference)
this way you will need to allocate the result array outside the function
and you will be safe