"invalid types 'int[int]' for array subscript" error

Hi guys,
I’m making a program to send a message through a led and read it on the other side with a photosensor. I have an issue that seem to be quite common but very specific at the same time, here is the code :

int sensorPin = A0;
int ledPin = 2;
int sensorValue = 0;
int listeOG[6] = {1,0,1,1,0,0}, i = 0;
bool verif = true;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  verif = true;
  i = 0;

  while (verif == true and i < 6) {
    if (message()[i] == listeOG[i]) {
      verif = true;
    } else {
      verif = false;
    }
    i++;
  }
}

int message() {
  int listeR[6], sensor = 0;
  
  digitalWrite(ledPin, HIGH);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[0] = testLum(sensor, 900);
  
  digitalWrite(ledPin, LOW);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[1] = testLum(sensor, 900);
  
  digitalWrite(ledPin, HIGH);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[2] = testLum(sensor, 900);
  
  digitalWrite(ledPin, HIGH);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[3] = testLum(sensor, 900);
  
  digitalWrite(ledPin, LOW);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[4] = testLum(sensor, 900);
  
  digitalWrite(ledPin, LOW);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[5] = testLum(sensor, 900);

  return listeR;
}

int testLum(int value, int palier) {
  if (value < palier) {
    return 0;
  } 
  else {
    return 1;
  }
}

and when I run it I get this error : “invalid types ‘int[int]’ for array subscript”.
I’ll be pleased with any of your help, thanks!

if (message()[i]Hard to see what you intended or hoped for there.

Your function "message" promises to return an int, but appears to attempt to return an array.

Thanks for the quick answer,
I played around with the code after sending that request, I’ve tried changing stuff about pointers and adresses but nothing seemed to resolve this issue. I then took into account your response and ditched the local variable “listeR[6]” for a global variable array and it worked!
Thanks for the help !

int listeOG[6] = {1,0,1,1,0,0}, i = 0;

It looks like the compiler is thinking you want 'i' declared as an array of 6 integers. Move the declaration of 'i' to its own line to make it a plain 'int' and that should help.

johnwasser:

int listeOG[6] = {1,0,1,1,0,0}, i = 0;

It looks like the compiler is thinking you want 'i' declared as an array of 6 integers. Move the declaration of 'i' to its own line to make it a plain 'int' and that should help.

?

Yep, missing a semicolon here, and both can be byte (vs int)

byte listeOG[6] = {1,0,1,1,0,0};
byte i = 0;

This
message()
seems to result in this
return listeR;

So how would this work

if (message()[i] == listeOG[i]) {

CrossRoads:
This
message()
seems to result in this
return listeR;

So how would this work

if (message()[i] == listeOG[i]) {

This is a java syntax

This version compiles. It uses a global instead of trying to have a function return the address of a local variable. I suppose making the local variable ‘static’ might also work.

int sensorPin = A0;
int ledPin = 2;
int sensorValue = 0;
int listeOG[6] = {1, 0, 1, 1, 0, 0};
int listeR[6];
int i = 0;
bool verif = true;


void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}


void loop()
{
  verif = true;
  i = 0;


  while (verif == true and i < 6)
  {
    message();  // Read listR
    if (listeR[i] == listeOG[i])
    {
      verif = true;
    }
    else
    {
      verif = false;
    }
    i++;
  }
}


void message()
{
  int sensor = 0;


  digitalWrite(ledPin, HIGH);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[0] = testLum(sensor, 900);


  digitalWrite(ledPin, LOW);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[1] = testLum(sensor, 900);


  digitalWrite(ledPin, HIGH);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[2] = testLum(sensor, 900);


  digitalWrite(ledPin, HIGH);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[3] = testLum(sensor, 900);


  digitalWrite(ledPin, LOW);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[4] = testLum(sensor, 900);


  digitalWrite(ledPin, LOW);
  delay(100);
  sensor = analogRead(sensorPin);
  listeR[5] = testLum(sensor, 900);


  return;
}


int testLum(int value, int palier)
{
  if (value < palier)
  {
    return 0;
  }
  else
  {
    return 1;
  }
}