为什么当我更改 rows 和 colm 的输入时它们会变成随机数

问题描述 投票:0回答:1

你好,我一直在模拟火灾在森林中蔓延,我基本上已经完成了,唯一的问题是当我切换行和列的数量时,它们突然变成了巨大的数字,我确信我错过了一些简单的东西,但我就是不能弄清楚吧

#include <stdio.h>

void InfoInput(int *rows, int *colm, int *seed, int *fireProb){
    FILE *file;
    char line[256];
    file = fopen("Test.txt", "r");
    if (file ==NULL){
        printf("fatal error\n");
    }
    
    fscanf(file, "%d",rows);
    fscanf(file, "%d",colm);
    fscanf(file, "%d",seed);
    fscanf(file, "%d",fireProb);
}

void treeInput(char forest[][102], int rows, int colm, int *totTrees){
    FILE *file;
    char line[256];
    file = fopen("Test.txt", "r");
    if (file ==NULL){
        printf("fatal error\n");
    }
    
    char temp;
    int x=0, tempRow, tempColm;
    while(x<100){
        fscanf(file, " %c", &temp);
        if(temp == 'Q'){
            x=1;
            break;
        } else if(temp == 'A'){
            for(int i = 0; i<rows; i++){
                for(int j = 0; j<colm; j++){
                    forest[i][j] = 'T';
                }
            }
        } else if (temp == 'T'||temp == 'E'||temp == 'F'){
            fscanf(file, "%d", &tempRow);
            fscanf(file, "%d", &tempColm);
            if(temp == 'T'){
                forest[tempRow-1][tempColm-1] = 'T';
            } if(temp == 'F'){
                forest[tempRow-1][tempColm-1] = 'F';
            }
            else {
                forest[tempRow-1][tempColm-1] = '.';
            }
        }
    }
    for(int i = 0; i<rows; i++){
        for(int j = 0; j<colm; j++){
            if (forest[i][j] == 'T'){
                *totTrees += 1;
            }
        }
    }
   
}

void printInfo(char forest[][102], int rows, int colm, int seed, int fireProb, int totTrees){
    printf("S. Madden 9/20\n\n");
    printf("rows: %d\n", rows);
    printf("cols: %d\n", colm);
    printf("trees: %d\n",totTrees);
    printf("seed: %d\n", seed);
    printf("probability: %d%%\n\n",fireProb);
   
}


int main()
{
    FILE *file;
    char line[256];
    file = fopen("Test.txt", "r");
    if (file ==NULL){
        printf("fatal error\n");
    }
    
    int rows, colm, seed, fireProb, Time=0;
    int totTrees = 0;
    
    InfoInput(&rows, &colm, &seed, &fireProb);
    //manualInput(&rows, &colm, &seed, &fireProb);
    
    char forest[rows][colm];
    
    treeInput(forest, rows, colm, &totTrees);
    
    printInfo(forest, rows, colm, seed, fireProb, totTrees);

    return 0;
}

这是我调用的文件

8
20
28
75
A
F 3 4
E 1 12
E 2 12
E 3 12
E 4 12
E 4 13
E 4 14
E 5 12
E 5 15
E 6 12
E 1 16
E 2 16
E 3 16
E 4 16
E 7 1
E 7 2
E 7 3
E 7 4
E 8 3
Q

输出是

S. Madden 9/20

rows: 777278548
cols: 1414812756
trees: 141
seed: 28
probability: 75%

当有 9 行和 20 列时(我正在测试它),这个程序可以完美运行,但是当我将其切换到 8 时,它就会变得随意 (顺便说一句,这是我发现问题的代码片段,还有更多,但对于这个问题来说并不重要)

老实说,我不知道问题是什么,所以感谢任何帮助

arrays c function
1个回答
0
投票

因为你在 main 中声明了

forest
并具有动态数量的行和列......

char forest[rows][colm];

然后您可以在具有固定列数的函数签名中使用它。

char forest[][102]

这会导致 C 的读写超出分配给

forest
的边界,覆盖其他变量中存储的数据。


例如,可以说...

int rows = 3;
int colm = 4;
char forest[rows][colm];

forest
分配了一大块 3 x 4 = 12 字节宽的数据。之后可能会立即分配其他一些变量内存。

___forest___
            __other__
123456789012345678901
^   ^   ^
^   ^   forest[2][0]
^   ^ 
^   forest[1][0]
^
forest[0][0]

为了简洁起见,我将使用

forest[][10]
。如果您尝试以
forest[][10]
的形式访问它,则表示每行有 10 字节宽。
forest[0][0]
相同,但
forest[1][0]
现在指向 11 个字节,而
forest[2][0]
指向 21 个字节。

___forest___
            __other__
123456789012345678901
^         ^         ^
^         ^         forest[2][0]
^         ^ 
^         forest[1][0]
^
forest[0][0]

如您所见,访问

forest[][10]
会渗入其他人的数据。

© www.soinside.com 2019 - 2024. All rights reserved.