I have created an empty char
multidimensional array, but when I try to change a specific value, it sometimes duplicates to another space in the array.
Example:
#include <iostream>
using namespace std;
char arr[2][2] = { 0 };
int main ()
{
arr[2][0] = 'A';
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout << "arr[" << i << "][" << j << "] = " << arr[i][j] << endl;
}
}
}
Output:
arr[0][0] =
arr[0][1] =
arr[0][2] =
arr[1][0] =
arr[1][1] =
arr[1][2] = A
arr[2][0] = A
arr[2][1] =
arr[2][2] =
The character A
should only appear in [2][0]
but it also appears in [1][2]
.
This happens only in these spaces:
[1][0], [2][0], [0][2], [2][2]
I was able to recreate this with a bigger array, but I can't say the specific values.
I have tried defining inside the main()
function but it created another problem, random characters started appearing in random locations of the array.
I tried initializing the array with char arr[2][2] = { 0 }
, but it didn't help.