Strcpy () function changing value of integer array?

I will start with the code I have, where input is a variable provided by the user:

int current[2] = {-1, -1}, next[2] = {-1, -1};
char *strtok_result = strtok(input, " ");
int i = 0;
while(strtok_result != NULL){
    i++;
    int count = 0;
    char strtok_buffer[2];
    printf("iteration %d-%d: next[0] = %d\n", i, ++count, next[0]);
    strcpy(strtok_buffer, strtok_result);
    printf("iteration %d-%d: next[0] = %d\n", i, ++count, next[0]);

    current[0] = next[0];
    current[1] = next[1];
    next[0] = strtok_buffer[1] - 48;            // ascii conversion, digit to column
    next[1] = toupper(strtok_buffer[0]) - 64;   // --- || ---, letter to row
    printf("iteration %d-%d: next[0] = %d\n\n", i, ++count, next[0]);
    strtok_result = strtok(NULL, " ");
}
return 0;

If I enter "A1 B2 C3", I expect the following output:

iteration 1-1: next[0] = -1
iteration 1-2: next[0] = -1
iteration 1-3: next[0] = 1

iteration 2-1: next[0] = 1
iteration 2-2: next[0] = 1
iteration 2-3: next[0] = 2

iteration 3-1: next[0] = 2
iteration 3-2: next[0] = 2
iteration 3-3: next[0] = 3

The result obtained, however, is as follows:

iteration 1-1: next[0] = -1
iteration 1-2: next[0] = -256
iteration 1-3: next[0] = 1

iteration 2-1: next[0] = 1
iteration 2-2: next[0] = 0
iteration 2-3: next[0] = 2

iteration 3-1: next[0] = 2
iteration 3-2: next[0] = 0
iteration 3-3: next[0] = 3

It seems to me that at runtime * strcpy (strtok_buffer, strtok_result); * the value of next [0] changes. It amazes me. I used to see similar results, and this is due to memory overlap (term?), But I don’t understand why this is happening here.

Any help is appreciated; I looked at myself blind, trying to figure it out.

Notes:

  • Confirmed input in the format "XY XY ...", where X is alpha and Y is a digit.
  • current [0], [1] [1] . , 2x36 .
+4
1

strtok_buffer[2] , "A1", "B2" "C3". , .

+5

All Articles