krishnanshu11@gmail.com
Dropped 8 months ago,
Last activity 8 months ago
1 answers
Once i is 3 you're reading past the end of the array. Neither of your two conditions prevent that from happening. Doing so triggers undefined behavior in your code.
When you have undefined behavior, there is no guarantee what your code will do. It might crash, it might output strange results (as the case of your alternate definition does), or it might appear to work properly (as it does in your original code).
You need to pass in the size of the array to the function, then use that to determine when to stop iterating.