Why is My C Code Unexpectedly Changing Other Elements in a 2D Array?

  • Thread starter Thread starter kyacout
  • Start date Start date
  • Tags Tags
    Array Change
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
kyacout
Messages
1
Reaction score
0
Code:
if(gameBoardState[i][col] == 0)
        {
            gameBoardState[i][col] = playerTurn;
            break;
        }

This code is supposed to change the values in gameBoardState[col] (a 2D array), but it changes values in other elements too. For example, when col = 0, and i = 5, gameBoardState[4][6] is changed with the same value as well.
 
Physics news on Phys.org
kyacout said:
Code:
if(gameBoardState[i][col] == 0)
        {
            gameBoardState[i][col] = playerTurn;
            break;
        }

This code is supposed to change the values in gameBoardState[col] (a 2D array), but it changes values in other elements too. For example, when col = 0, and i = 5, gameBoardState[4][6] is changed with the same value as well.


How are you sure it's getting changed by this piece of code & not elsewhere?
Did you check this in the debugger?

Also what's the datatype of gameboardState, playerTurn, i & col?