Why do the global variables row and col restore the initial values of 2 and 1 every time they are used

topic description

in fact, there is no need to look at the title. Search UVa227 and you can see the title

.

sources of topics and their own ideas

Puzzle, ACM/ICPC World Finals 1993, UVa227

related codes

/ / Please paste the code text below (do not replace the code with pictures)

-sharpinclude <stdio.h>
-sharpinclude <string.h>
-sharpdefine len 6
char matrix[len][len] = {
        "TRGSJ", "XDOKI", "M VLN", "WPABE", "UQHCF"
};
int flag = 1, row = 2, col = 1;
void Above(char matrix[len][len], int row, int col) {
    if (row > 0 && row <= 4) {
        matrix[row][col] = matrix[--row][col];
        matrix[row][col] = " ";
    } else flag = 0;
}
void Below(char matrix[len][len], int row, int col) {
    if (row >= 0 && row < 4) {
        matrix[row][col] = matrix[PProw][col];
        matrix[row][col] = " ";
    } else flag = 0;
}
void Left(char matrix[len][len], int row, int col) {
    if (col > 0 && col <= 4) {
        matrix[row][col] = matrix[row][--col];
        matrix[row][col] = " ";
    } else flag = 0;
}
void Right(char matrix[len][len], int row, int col) {
    if (col >= 0 && col < 4) {
        matrix[row][col] = matrix[row][PPcol];
        matrix[row][col] = " ";
    } else flag = 0;
}
int main() {
    char ins[100]; // instruction
    gets(ins);
    for (int i = 0; i < strlen(ins); PPi) {
        if (ins[i] == "0") break;
        if (ins[i] == "A") {Above(matrix, row, col);continue;}
        else if (ins[i] == "B") {Below(matrix, row, col);continue;}
        else if (ins[i] == "L") {Left(matrix, row, col);continue;}
        else if (ins[i] == "R") {Right(matrix, row, col);continue;}
        else flag = 0;
    }
    if (flag) {
        for (int j = 0; j < len - 1; PPj) {
            printf("\n");
            for (int k = 0; k < len - 1; PPk)
                printf("%c ", matrix[j][k]);
        }
    } else printf("This puzzle has no final configuration.");
    return 0;
}

what result do you expect? What is the error message actually seen?

row and col restore 2, 1
when calling Above, Below, Left, and Right. How to keep row and col modified after calling?

CPP c
May.14,2022

https://blog.csdn.net/sinat_3...

this should be your question.


function parameters are also global variables?!! The first day of learning programming?


I can irresponsibly say that I didn't finish reading your code. Irresponsible guessing can be solved with pointer input parameter (fun (& row,&col)). If you simply (fun (row,col)) without a pointer, you just copy their values, row and col are just local variables, and the stack of this function is cleared after each fun return, and the outer row and col are not modified. Then row and col are the initial values when they are again used as arguments to other functions.
it is recommended that landlords learn about stacks and pointers. Row and col in the


function parameters hide global variables with the same name, so row and col represent these two parameters in the scope of the function, not the two global variables defined in the header of the file. The landlord needs to know about the scope first.

Menu