Very simple question about array [invalid types 'int[int]' for array subscript]

Hello, I feel a little bit embarassed to post a so simple question, that is more related to c++ programming, but after a few hours googlizing I haven't yet found a simple answer to this simple question...
Here it is, I'm trying to make an ARP like table... that means that I want to dynamically write into an array.

So the most basic way to do this should be something like that :

int k;
int tab[256];

void init_tableau (int tab)
  {
  for (k=0; k<255; k=k+1)
    {
    tab[k]=0;
    }
  }

But this should not be authorized to do this because I get the error : "invalid types 'int[int]' for array subscript"

So my question is : What is blocking ? Is that a restriction of c++ ?
The bonus question is : Is there a mean to do this kind of manipulation with the arduino ?

If tab is a global variable you do not need to pass it as a parameter. Your local definition is an int and is overriding the global declaration, which is the array.

You have a global variable, tab, of type int array.

You have a local variable (the function argument, tab, of type int. It is that local variable that you are trying to treat as an array.

1 Like

macgi:

void init_tableau (int tab)

The formal argument tab is an int. You are trying to treat it as an array of ints. An array is syntactically equivalent to a pointer to an array element, so change your declaration to void init_tableau (int *tab) and it should work.

Thanks all, those answers where perfects, and the use of pointer works nicelly.

Thanks again.

Read up on the scope of variables in c/c++.

Very helpful answer, PeterH. I have been learning about pointers and references whilst learning openFrameworks and C++, but didn't think I'd need any of that in my simple Arduino sketch, below.

PeterH:
The formal argument tab is an int. You are trying to treat it as an array of ints. An array is syntactically equivalent to a pointer to an array element, so change your declaration to void init_tableau (int *tab) and it should work.

So is it the case what when using an array as a function parameter, it must always be used as a pointer (*arrayName)? I was getting the same error as the OP, which led me to this thread.

int redPin = 9;
int greenPin = 10;
int bluePin = 11;


int white[] = {200, 255, 255};

int colourName;

void setup() {
  Serial.begin(9600);
}

void loop() {
  
myColour(white);

}

void myColour(int *colourName) {
  analogWrite(red, colourName[0]);
  analogWrite(green, colourName[1]);
  analogWrite(blue, colourName[2]);
}