How to count array elements

Please don't tell me how common this question is.

Alright, so if I have an array like this:

int array[12] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};

...then how do I figure out where the 1 is as an int value? Like how do I convert that into just saying 3, as in the third element is the 1. If the fifth element was the one, the value would be 5.

Obviously a for() loop function is used, but I was unable to get the following code with 3 values read from buttons working:

int read1;
int read2;
int read3;

int read;

int val;

int readArray[3] = {read1, read2, read3};

int readFunct(){
  for (int i; i >= 2; i++){
    if (readArray[i] = 1){
      read = i;
      return read;
      break;
    }
  }
}

void setup()
{
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  
  Serial.begin(9600);
}

void loop()
{
  read1 = digitalRead(10);
  read2 = digitalRead(11);
  read3 = digitalRead(12);
  
  readFunct();
  
  Serial.println(read);
}

EDIT - If you're wondering what it does, it just reads 0s.

You need == not = in a comparison.

...it compiled...

EDIT - The fix didn't work.

Its index in the array is 2.

Welcome to C/C++

Also, check the reference for for loop syntax.

Yes. And it is doing exactly what you told it to.

Just because something compiles doesn’t mean it is doing what you think.

Focus. ‘=‘ for assignment. ‘==‘ for test of equality.

a7

Most useless post today.

Irrelevant at the moment. And I don't see what's wrong with the loop.

And yeah saying that it still didn't work was pointless but someone in this thread doesn't know that.

You're in a minority.

OK, talk me through it.
Describe it.

As the value "i" counts up, the loop checks each value in the array for a 1. If there's a 1, the loop sets the value "read" to the amount of counts taken, returns the value, and then breaks off.

And that value is . . ?

What about the condition in the middle?

"i" is the counter the for loop uses as reference. The condition is checking for a 1.

No, what is the value of "i", at the start of the loop?

What about the condition?

What the microcontroller prefers at that specific moment :rofl: :rofl:

I prefer "what history decrees" - more poetic.

Undefined? I'm assuming there's a counting error somewhere.

Should I set "i" to be 0?

EDIT - I'm going to serial print "i".

What happened when you tried?

What about the condition?

What condition? The if?

No, the condition in the middle of the for expression.

nice, just in case return fails, always good to have contingency

@anon73444976 The only conditional is the if. The condition is "if a 1 is found in the element of the array that equals the current counter state".