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

In summary, the code is designed to change the values in gameBoardState[i][col] of a 2D array, but it is also inadvertently changing values in other elements. This can be seen when col = 0 and i = 5, as gameBoardState[4][6] is changed with the same value. It is uncertain if this is the only cause of the issue, and it is recommended to check with a debugger. The data types used in this code are not specified.
  • #1
kyacout
2
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.
 
Technology news on Phys.org
  • #2
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?
 

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

1. What is an unexpected array change in C?

An unexpected array change in C refers to a situation where the elements or size of an array are altered unexpectedly during program execution. This can happen due to various factors such as uninitialized variables, memory corruption, or incorrect indexing.

2. How can I prevent unexpected array changes in C?

To prevent unexpected array changes in C, it is important to ensure that all variables and arrays are properly initialized before use. It is also crucial to use appropriate memory management techniques and validate all array indexes to avoid any potential memory corruption.

3. What are some common causes of unexpected array changes in C?

Some common causes of unexpected array changes in C include uninitialized variables, incorrect use of pointers, buffer overflows, and memory corruption. These issues can arise due to programming errors or external factors such as hardware failures.

4. How can I debug unexpected array changes in C?

To debug unexpected array changes in C, you can use various debugging tools such as a debugger or a memory analyzer. These tools can help you track down the source of the issue and identify any errors in your code that may be causing the unexpected array changes.

5. Is there any way to recover from an unexpected array change in C?

In most cases, it is not possible to recover from an unexpected array change in C. However, you can try to minimize the impact by implementing proper error handling techniques, such as using try-catch blocks, to gracefully handle any unexpected changes and prevent your program from crashing.

Similar threads

  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
7
Replies
235
Views
10K
  • Programming and Computer Science
Replies
34
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
Back
Top