简体   繁体   English

二维整数C数组上的奇异值

[英]Strange values on a 2-d integer C array

I know this is probably a very simplistic fix, but I have spent an hour googling and trying to fix it to no avail. 我知道这可能是一个非常简单的修复程序,但是我花了一个小时的时间进行谷歌搜索,并试图将其修复为无济于事。 This is the code in the beginning of main. 这是main开头的代码。 Nothing else manipulates the graph[][] array. 没什么可以操纵graph [] []数组的。 Its very simple C code. 它非常简单的C代码。 I am trying to initialize a 2-d integer array, and set all values to 0. But Later values end up being strange, random numbers. 我试图初始化一个2-d整数数组,并将所有值设置为0。但是后来的值最终是奇怪的随机数。 I know this happens when you don't initialize every value. 我知道当您不初始化每个值时都会发生这种情况。 BUT I am, and I ran through GDB and confirmed that it went through my test 4X4 array and initialized everything to 0. I am stumped. 但是我是,我通过GDB并确认它通过了我的测试4X4数组并将所有内容初始化为0。我很困惑。

printgrapharray is just a double for loop to go through every slot... printgrapharray只是通过每个插槽的双for循环...

code: 码:

#include <stdio.h>

#define inputarraylength 200
#define MAXVERTICES 100

void printgrapharray(int graph[][MAXVERTICES], int vertamount);

int main(int argc, char* argv[])
{

    int numvert = 1;
    char edges[inputarraylength];
    char vertices[inputarraylength];

    freopen("input.txt", "r", stdin);

    scanf("V={%[^}]s", vertices);

    printf("\nVERTICES\n");
    printf("%s\n", vertices);            //comma-seperated list of names

    for (int i = 0; i < inputarraylength; i++) {

        if (vertices[i] == ','){

            numvert++;                          //get number of vertices
        }
    }
    const char *verts[numvert];         //create vertice array

    printf("numvert: %d\n", numvert);

    int graph[numvert][numvert];                    //create n*n matrix array

    for (int k = 0; k < numvert; k++) {
        for (int z = 0; z < numvert; z++) {

            graph[k][z] = 0;                            //init the graph 2-d array slots to 0
        }
    }
    printgrapharray(graph, numvert);
}

output: 输出:

VERTICES
Aa, Bbb, Cccc, Ddddd
numvert: 4

ADJACENCY MATRIX!
0 0 0 0 
0 0 0 0 
1562140139 32767 1562140159 32767 
774977075 1163132977 1398754642 1230197573 

You declare the printgrapharray function like 您可以像这样声明printgrapharray函数

void printgrapharray(int graph[][MAXVERTICES], int vertamount);

which means the argument graph is a pointer to arrays of MAXVERTICES integers. 这意味着参数graph是指向MAXVERTICES整数数组的指针。

When you call the function you pass the main local variable graph to it, which will decay to a pointer to arrays of numgraph integers. 调用该函数时,将main局部变量graph传递给该函数,该图将衰减为指向numgraph整数数组的指针。 Unless MAXVERTICES and numgraph are equal, you have a mismatch in the types, and that will lead to undefined behavior . 除非MAXVERTICESnumgraph相等,否则类型将不匹配,这将导致不确定的行为

This problem is something the compiler should have detected, and given you a warning about. 该问题是编译器应已检测到的,并向您发出警告。


There is actually a simple solution to this, because C has variable-length arrays: Use the argument vertamount in the declaration of the graph argument: 实际上,有一个简单的解决方案,因为C具有可变长度的数组:在graph参数的声明中使用vertamount参数:

void printgrapharray(int vertamount, int graph[][vertamount]);

Note that the order of the arguments had to be switched, because vertamount must be declared before it is used. 请注意,必须切换参数的顺序,因为必须在使用vertamount之前声明其声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM