0
int sum = 3;
int i = 0;
myCode[] = {};
void loop () {
  myCode[i] = sum;
}

In this example the variable 'i' gets the value of '3' instead of assigning the value of '3' to the array myCode[] with the index '0' (i).

I honestly don't know why it does this. This is only a small part of the program. I don't include the full program because that would only be confusing for you guys and this is the only part of the program that is not working.

How would I use the value of i to assign a value to the array with i as index??

too honest for this site
  • 12,050
  • 4
  • 30
  • 52

1 Answers1

0

It looks like you see undefined behavior, because myCode array does not have enough space. Since you see myCode[0] modifying the value of i instead of myCode, the size of myCode in your compiled program is zero, and its address is the same as i's address.

Fixing this problem requires allocating myCode at the max size that you expect it to have:

int myCode[20] = {0};

Now the code is going to work correctly for indexes between 0 and 19, inclusive.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • So I want my array to have max 4 integers so I changed: int myCode[] = {}; to: int myCode[4] = {}; I'm not sure what the {0} does but my program seems to work so thanks alot. –  Oct 10 '16 at 17:56