# Problem with array indexing on nano

**URL:** <https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569>\
**Category:** Programming\
**Created:** [April 22, 2025, 2:16am UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569 "2025-04-22T02:16:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![docjmac](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docjmac/32/1130437_2.png) [@docjmac](https://forum.arduino.cc/u/docjmac)\
**Post date:** [April 22, 2025, 2:16am UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/1 "2025-04-22T02:16:29Z")

</div>

I am porting working code from a UNO R4 WIFI to a nano and seem have a very odd problem. The value of the last ( and sometimes first) value of the array gets corrupted. That is, the second time I access the array the values are changed from the value originally set. In the code below I've stripped out almost everything to illustrate the problem but will reiterate: this code works perfectly on the UNO R4.  
I thought the first nano was defective, tried another - same result. This is the first time I've worked with a nano so there maybe some idiosyncrasy I'm not expecting.  
Any help would be appreciated.

```arduino
int maxPow;
int pow2;
int minPin;
int maxPin = 8;
int currentVal;
int maxVal = 15;
int maxIndex = 3;

void setup() {
  Serial.begin(9600);
  maxPow = log(maxVal)/log(2);

  minPin = maxPin - maxPow;
  Serial.println("maxPow = " + String(maxPow, DEC) + " minPin = " + String(minPin, DEC));  
  delay(longPulse);
  int i;
  int j;
  int k;
  int pins[maxIndex];

  //Create array and print it out immediately
  for(j=0;j<=maxIndex;++j){
    pins[j] = maxPin - j;
    Serial.println("first time: pins[" + String(j, DEC) + "]: " + String(pins[j], DEC));
  }

  // Print it out again
  for(k=0;k<=maxIndex;++k){
    Serial.println("second time: pins[" + String(k, DEC) + "]: " + String(pins[k], DEC));
}

}

void loop() {

}

```

Should create a 4 element array with the numbers 8-5 descending, then print it out immediately as created and then in a second for loop.  
Should be super simple.

This is the result from the nano:  
first time: pins[0]: 8  
first time: pins[1]: 7  
first time: pins[2]: 6  
first time: pins[3]: 5  
second time: pins[0]: 8  
second time: pins[1]: 7  
second time: pins[2]: 6  
second time: pins[3]: 520

Result from the UNO:  
first time: pins[0]: 8  
first time: pins[1]: 7  
first time: pins[2]: 6  
first time: pins[3]: 5  
second time: pins[0]: 8  
second time: pins[1]: 7  
second time: pins[2]: 6  
second time: pins[3]: 5

One more thing - when I change the creation of the array to a single line and then print it out the first for loop also fails:

int pins[maxIndex] = {8,7,6,3};  
for(j=0;j\<=maxIndex;++j){  
//pins[j] = maxPin - j;  
Serial.println("first time: pins[" + String(j, DEC) + "]: " + String(pins[j], DEC));  
}

for(k=0;k\<=maxIndex;++k){  
Serial.println("second time: pins[" + String(k, DEC) + "]: " + String(pins[k], DEC));  
}

produces  
first time: pins[0]: 8  
first time: pins[1]: 7  
first time: pins[2]: 6  
first time: pins[3]: 520  
second time: pins[0]: 8  
second time: pins[1]: 7  
second time: pins[2]: 6  
second time: pins[3]: 520

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [April 22, 2025, 2:22am UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/2 "2025-04-22T02:22:26Z")

</div>

Wrap all your code in code blocks.

 ![paste](https://europe1.discourse-cdn.com/arduino/original/4X/c/b/4/cb40e1987fa5e6f7f7cc8197b07ef618b42f4a73.png)

---

<div class="post-metadata">

**Author:** ![docjmac](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docjmac/32/1130437_2.png) [@docjmac](https://forum.arduino.cc/u/docjmac)\
**Post date:** [April 22, 2025, 2:27am UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/3 "2025-04-22T02:27:16Z")

</div>

It is in a code block.  
The second set of code is not complete, it's just for clarification.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [April 22, 2025, 2:38am UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/4 "2025-04-22T02:38:07Z")

</div>

```arduino
  int pins[maxIndex];

  //Create array and print it out immediately
  for(j=0;j<=maxIndex;++j){
    pins[j] = maxPin - j;
    Serial.println("first time: pins[" + String(j, DEC) + "]: " + String(pins[j], DEC));
  }

```

maxIndex is 3.  
The pins array therefore has 3 elements.  
In the for loop, pins[j] with j = maxIndex is writing to the 4th element of a 3-element array.

You likely did not notice on the Uno R4 WiFi because whatever was being overwritten was not obvious.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [April 22, 2025, 3:30am UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/5 "2025-04-22T03:30:56Z")

</div>

> [@docjmac](#):
>
> It is in a code block

No. You missed some. Follow the guidelines.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [April 22, 2025, 6:11am UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/6 "2025-04-22T06:11:00Z")

</div>

> [@docjmac](#):
>
> `int pins[maxIndex];`

So if you really want the last index to be maxIndex, as numbering starts at 0, you need maxIndex+1 éléments in the array.

Define it like this

`int pins[maxIndex+1];`

Then your for loops are legit.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [April 22, 2025, 11:16am UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/7 "2025-04-22T11:16:25Z")

</div>

Some might say instead of going around with _maxIndex_ being the last legit number usable as an array index, the variable should be named something like _numberOfElements._ Which I'll cut short to _nElements_ below. Name it better.

Then arrays are dimensioned naturally:

```arduino
int someArray[nElements];

```

And _for_ loops take an idiomatic pattern C/C++ programmers recognize as doing something N times, or doing something for N things numbered from zero to N - 1. Remarkable is the absence of any addition or subtraction to "correct" things that would otherwise be off by one.

```arduino
  for (byte ii = 0; ii < nElements; ii++) {
    Serial.println(someArray[ii]):
  }

```

It's just more of getting accustomed to thinking about 0 as a perfectly good number and the way arrays work in C/C++.

The final trick is letting the compiler count the elements

```arduino
const byte inputPins[] = {2, 3, 6, 7, A0, };
const byte nInputs = sizeof inputPins / sizeof *inputPins;

```

And use _nInputs_, the number of elements in the array at indices 0 .. (_nInputs_ - 1) everywhere instead of the literal number you used to initialise your _numberOf_ or _maxIndex_ variable.

a7

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [April 22, 2025, 12:09pm UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/8 "2025-04-22T12:09:36Z")

</div>

I told chatGPT to rewrite my post and gave it no specific goals. Here's what it said I said. 😑

Some people argue that, instead of using a variable like `maxIndex` to represent the highest usable array index, a better name would be something like `numberOfElements` — let’s say `nElements` for short.

The beauty of this approach is that arrays become easier to reason about:

```arduino
int someArray[nElements];

```

And the loop pattern that follows is instantly familiar to any C or C++ programmer. It's the classic idiom for doing something _n_ times — or operating on _n_ items, indexed from 0 to `n - 1`:

```arduino
for (byte ii = 0; ii < nElements; ii++) {
  Serial.println(someArray[ii]);
}

```

No `-1`s, no `+1`s, no correcting for an off-by-one mistake you already made but haven’t found yet. It just works. The loop does exactly what it says: starts at zero, runs while less than `nElements`.

It's really just a matter of getting used to thinking of `0` as a perfectly fine number — as a _valid_ place to start — and embracing how arrays naturally behave in C and C++.

And then there’s the ultimate move: let the compiler do the counting for you.

```arduino
const byte inputPins[] = {2, 3, 6, 7, A0};
const byte nInputs = sizeof inputPins / sizeof *inputPins;

```

Now `nInputs` holds the number of elements in the array — automatically. You never have to count by hand, and you never need to go back and change a literal if the array grows.

And everywhere you would have used a `magic number` or a questionable `maxIndex`, just use `nInputs` — because that’s what you really meant: the number of valid entries in the array, accessible from index `0` through `nInputs - 1`.

chatGPT

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [April 22, 2025, 1:44pm UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/10 "2025-04-22T13:44:34Z")

</div>

> You can clearly see that in the output I provided.

You will get better results when you ask for clarification, rather than getting snappy, like you did at me for telling you to format your code (which you did not). BUT, that happens when you first post on the forum... so, please, correct the code not in the code block, like we all do... and your problems will be solved.

---

<div class="post-metadata">

**Author:** ![docjmac](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docjmac/32/1130437_2.png) [@docjmac](https://forum.arduino.cc/u/docjmac)\
**Post date:** [April 22, 2025, 3:52pm UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/11 "2025-04-22T15:52:25Z")

</div>

Nope - you were right. I looked at that for hours and just missed it.

---

<div class="post-metadata">

**Author:** ![dilberto](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dilberto/32/1126465_2.png) [@dilberto](https://forum.arduino.cc/u/dilberto)\
**Post date:** [April 22, 2025, 4:40pm UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/12 "2025-04-22T16:40:39Z")

</div>

Bravo!

That's the way I do things!

I couldn't help but remember the quote:

“Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration.” - Stan Kelly-Bootle

---

<div class="post-metadata">

**Author:** ![dilberto](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dilberto/32/1126465_2.png) [@dilberto](https://forum.arduino.cc/u/dilberto)\
**Post date:** [April 22, 2025, 4:48pm UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/13 "2025-04-22T16:48:52Z")

</div>

I'd go a step further:  
`
#define sizeOfArray(x) (sizeof((x)) / sizeof((x)[0]))`

`
`

`for (byte ii = 0; ii < sizeOfArray( someArray ) ; ii++) {
Serial.println(someArray[ii]):
}
`

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [April 22, 2025, 5:15pm UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/14 "2025-04-22T17:15:17Z")

</div>

> [@dilberto](#):
>
> My compromise of 0.5 was rejected...

_LOL_.

I have an uneasy feeling that if we hang on for a few more years arrays may indeed be indexed differently. 😑

a7

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [April 22, 2025, 11:17pm UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/15 "2025-04-22T23:17:19Z")

</div>

> [@dilberto](#):
>
> I'd go a step further:

Go all the way

```arduino
for (auto&& value : someArray) Serial.printn(value);

```

No index = no overflow 🙂

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 19, 2025, 11:18pm UTC](https://forum.arduino.cc/t/problem-with-array-indexing-on-nano/1375569/16 "2025-10-19T23:18:11Z")

</div>

Closed on October 19, 2025, 11:18pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
