Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 矩阵中的意外(意外)变化_C_Pointers_Matrix_Ram_File Handling - Fatal编程技术网

C 矩阵中的意外(意外)变化

C 矩阵中的意外(意外)变化,c,pointers,matrix,ram,file-handling,C,Pointers,Matrix,Ram,File Handling,我正在尝试用Dijkstra算法为一个学校项目做最短路径查找程序 我必须使用两个不同的文件,一个包含城市及其代码,例如 纽约1号、芝加哥2号等 另一个包含两个城市之间的距离,例如 纽约-芝加哥-789 我将城市名称保存在一个字符矩阵中,从不更改它,但在程序结束时,当我打印所有字符时,该矩阵有一些不同的字符,这导致邻域矩阵中缺少两个距离。我在程序中的不同位置打印了矩阵,以了解其损坏的位置。我认为当我调用addDistance函数时,它会损坏 PS:我在另一个函数中声明了城市名称矩阵,并将其分配给该

我正在尝试用Dijkstra算法为一个学校项目做最短路径查找程序

我必须使用两个不同的文件,一个包含城市及其代码,例如

纽约1号、芝加哥2号等

另一个包含两个城市之间的距离,例如

纽约-芝加哥-789

我将城市名称保存在一个字符矩阵中,从不更改它,但在程序结束时,当我打印所有字符时,该矩阵有一些不同的字符,这导致邻域矩阵中缺少两个距离。我在程序中的不同位置打印了矩阵,以了解其损坏的位置。我认为当我调用addDistance函数时,它会损坏

PS:我在另一个函数中声明了城市名称矩阵,并将其分配给该函数中的**矩阵变量

    void addToNM (){
        char ch;
        FILE *fp;
        fp = fopen("sehir mesafe.txt","r");

        int (*neighborhoodMatrix)[counter] = calloc(counter,sizeof (*neighborhoodMatrix)); /// Allocating memory to neighborhood matrix.
        char (*city) [15] = calloc(15,sizeof(*city)); /// Allocating memory to city matrix.
        char number[3];

        int z = 0, x = 0, y = 0;

        char **matrix = addToCCM();

        while((ch=fgetc(fp))!= EOF)
        {
            if (ch == '-')
            {
                if(x==1)
                {
                    continue;
                }
                else{
                    x++;
                }
                y = 0;
                continue;
            }
            else if (ch>=48 && ch<=57)
            {
                number[z]=ch; /// adding numbers to number array.
                z++;
                continue;
            }
            else if (ch == '\n')
            {
                addDistance(compare0(city,matrix,counter),compare1(city,matrix,counter),neighborhoodMatrix,atoi(number)); // I think corruption happens here. !!!
                for(int i=0; i<2; i++)
                {
                    for(int j=0; j<15; j++)
                    {
                    city[i][j] = '\0';
                    }
                }
                for(int i=0; i<3; i++){
                    number[i]= '\0';
                }
                x = 0;
                y = 0;
                z = 0;
                continue;
            }
            else{
                city[x][y] = ch;
                y++;
            }
        }
        fclose(fp);
}
void addToNM(){
char ch;
文件*fp;
fp=fopen(“sehir mesafe.txt”,“r”);
int(*邻域矩阵)[counter]=calloc(counter,sizeof(*邻域矩阵));///为邻域矩阵分配内存。
char(*city)[15]=calloc(15,sizeof(*city));///为城市矩阵分配内存。
字符数[3];
intz=0,x=0,y=0;
字符**矩阵=addToCCM();
而((ch=fgetc(fp))!=EOF)
{
如果(ch='-')
{
如果(x==1)
{
继续;
}
否则{
x++;
}
y=0;
继续;
}

如果(ch>=48&&chYou需要在这里显示a。我添加了打印的城市代码矩阵的屏幕截图。当它刚被分配并且调用addDistance函数后。Hmmm,1)如果
int compare0(char cm[][15],char ccm[][15],int counter)会发生什么情况
没有找到任何匹配项?没有返回。这可能吗?2)最好将
int ch
一起使用,而((ch=fgetc(fp))!=EOF)
3)使用
更容易理解,否则(ch>='0'&&ch=48&&ch
字符号[3];
不足以将
789
存储为字符串。
calloc(15,sizeof(*city))中的15
是可疑的,我希望
calloc(NUM\u OF_CITIES,sizeof(*city))
NUM\u OF_CITIES
大约是23个或更多。
x++
可以在
NUM OF_CITIES
之后出现,而没有检测到-它缺少范围检查。
int compare0 (char cm[][15],char ccm[][15],int counter){ /// finds the city number for city 1 and returns it.
    for(int i=1; i<counter; i++){
        if(!strcmp(cm[0],ccm[i])){ /// compares two strings given. ccm stands for cityCodeMatrix. cm stands for city
            return i;
        }
    }
}

    int compare1 (char cm[][15],char ccm[][15],int counter){ /// finds the city number for city 2 and returns it.
        for(int i=1; i<counter; i++){
            if(!strcmp(cm[1],ccm[i])){ /// compares two strings given. ccm stands for cityCodeMatrix. cm stands for city.
                return i;
            }
        }
    }

    void addDistance (int i, int j, int nm[][counter], int distance){ /// adds distance to neighborhood matrix. nm stands for neighborhood matrix.
        nm[i][j] = distance;
        nm[j][i] = distance;
    }