Array Error

Hello all,

I know this has been asked but I still do not understand the difference. When I compile the following code I repeatedly get the error:

error: invalid types ‘int[int]’ for array subscript

I think it has to do with the difference between int and int* (integer vs pointer) arrays, but I’m not sure how to fix this.

Thanks in advance.


int timer = 1500;
int hold = 3000;
int ledpin = 10;
int *floorarray[] = {0, 0, 0, 0};

void setup() {
  for (ledpin; ledpin<14; ledpin++){
    pinMode(ledpin, OUTPUT);
  }
  for (int buttonpin = 1; buttonpin<5; buttonpin++){
    pinMode(buttonpin, INPUT);
  }
}


int buttoncheck(){
  
  int buttonpin = 1;
  
  while (buttonpin<5){
    if (buttonpin = HIGH){
      *floorarray[buttonpin-1]=1;
      buttonpin = buttonpin + 1;
    }
  }
  return floorarray;
}


void loop() {

//up loop

  for (int i=1; i<4; i++){
    
//determine which, if any, floors have been requested 

    int a = buttoncheck();

//execute loop if floor pressed
    
    if(a[i]==1){
      digitalWrite(ledpin, LOW);
      delay(timer);
      ledpin = ledpin + 1;
      digitalWrite(ledpin, HIGH);
      delay(hold);
      a[i]=0;
    }

//execute loop if floor not pressed
    
    if(a[i]==0 && (a[i+1]==1 || a[i+2]==1)){
      digitalWrite(ledpin, LOW);
      delay(timer);
      ledpin = ledpin + 1;
      digitalWrite(ledpin, HIGH);
      delay(timer);
    }
  }

//down loop

  for (int i=3; i<4; i--){

//determine which floors, if any, have been selected

    int a = buttoncheck();

//execute loop if floor pressed
  
    if(a[i]==1){
      digitalWrite(ledpin, LOW);
      delay(timer);
      ledpin = ledpin - 1;
      digitalWrite(ledpin, HIGH);
      delay(hold);
      a[i]=0;
    }

//execute loop if floor not pressed
    
    if(a[i]==0 && (a[i-1]==1 || a[i-2]==1)){
      digitalWrite(ledpin, LOW);
      delay(timer);
      ledpin = ledpin - 1;
      digitalWrite(ledpin, HIGH);
      delay(timer);
    }
  }

//return to first floor if nothing selected

  int a = buttoncheck();
  if (a[0]==0 && a[1]==0 && a[2]==0 && a[3]==0){
    
    digitalWrite(ledpin, LOW);
    delay(timer);
    ledpin = ledpin - 1;
    digitalWrite(ledpin, HIGH);
    delay(timer);
  }
}

Don't put 0 to initialize your pointers in your array, use NULL instead (which is a pointer pointing "nowhere")

(I assume you understand that your floorarray array holds 4 pointers to integers, not integers, right? Your code seems to be confused later on... 8) )

  for (ledpin; ledpin<14; ledpin++){

You need to initialize ledpin in the same way as you initialize buttonpin in the next for loop.

Pete

J-M-L: (I assume you understand that your floorarray array holds 4 pointers to integers, not integers, right? Your code seems to be confused later on... 8) )

I suppose my code is as confused as I am.. care to elaborate?

I see you've initialized ledpin by declaring it to be 10. Then in setup() you use it in the for loop to increase it to 14. Later on, once the loop() function is executing, you keep increasing or decreasing ledpin depending up button states. You are going to have to figure out if the value of ledpin is really changing correctly. I don't think it is, but I'll let you sort it out.

Pete

el_supremo:

  for (ledpin; ledpin<14; ledpin++){

You need to initialize ledpin in the same way as you initialize buttonpin in the next for loop.

Pete

ledpin is already initialised to 10, but making that clear wouldn’t do any harm

el_supremo: You are going to have to figure out if the value of ledpin is really changing correctly. I don't think it is, but I'll let you sort it out.

You're right, I guess I hadn't looked at that too closely. Thanks!

I meant do you need pointers in that array or do you need integer values?

Sorry for the late reply. I'm not sure I quite understand the difference. I'm trying to update the values within the array to either 0 or 1 and call on them to check within my loop

Then why do you need an array of pointers?

Just an int floorArray[] = {0,0,0,0}; is what you need to declare your array of 4 values and then floorArray[2] = 1; will set the value. And given you store 0 or 1, no need for that array to hold int, a byte array is plenty: byte floorArray[] = {0,0,0,0};

Also your buttoncheck() function is retuning a pointer to the array instead of an int as you declare. Just declare the function as retuning nothing with void buttoncheck() an de don't do any return in the body of the function - your array is a global variable anyway