Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.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_Graph_Reference - Fatal编程技术网

C 通过引用传递变量的错误

C 通过引用传递变量的错误,c,graph,reference,C,Graph,Reference,我有一个相当简单的问题。我用C语言编写了一个计算机代码,它得到一个邻接矩阵形式的图,图中的每一条边都有一个与其相关联的数字,如果你愿意的话,还有一个权重 如果边的权重为0,则该边不存在。例如,如果在条目1中,邻接矩阵中的2是0,那么我们知道顶点1与顶点2没有连接 到目前为止还不错。我的问题是我试图计算有多少边,我得到了一个意外的值 int **get_adjacency_matrix(int* num_of_vertices,int *num_of_edges) { int i,j,k,

我有一个相当简单的问题。我用C语言编写了一个计算机代码,它得到一个邻接矩阵形式的图,图中的每一条边都有一个与其相关联的数字,如果你愿意的话,还有一个权重

如果边的权重为0,则该边不存在。例如,如果在条目1中,邻接矩阵中的2是0,那么我们知道顶点1与顶点2没有连接

到目前为止还不错。我的问题是我试图计算有多少边,我得到了一个意外的值

int **get_adjacency_matrix(int* num_of_vertices,int *num_of_edges)
{
    int i,j,k,**graph,rows=0;
    printf("Enter the number of vertices in your connected undirected graph:");
    scanf("%d",num_of_vertices);
    printf("\nVery important: I assume there are no parallel edges\n");
    rows=*num_of_vertices;
    graph=(int**)malloc(rows*sizeof(int*));
    for(i=0;i<rows;i++)
        graph[i]=(int*)malloc((rows-i)*sizeof(int));
    printf("Please Enter the upper triangular cost-adjacency matrix\n");
    for(i=0;i<rows;i++)
    {
        for(k=0;k<i;k++)
            printf("  "); //blank spaces for readability.
        for(j=0;j<rows-i;j++)
        {
            scanf("%d",&(graph[i][j]));
            if(graph[i][j]>0)
                (*num_of_edges)++; //edge found
        }
    }
    printf("*num_of_edges = %d",*num_of_edges);
    return graph;
}
那条线印的是垃圾。奇怪的是,num_of_顶点变量工作正常

因此,在**get_Adjacence_matrix程序中,我的边数(num_of_edges)得到了正确的值,但它将错误的值返回给void main

为什么?

编辑修改后的先前答案

声明

printf("there are % edges.\n",num_of_edges);

具有错误的格式说明符,应为%d。

顺便说一句,void main不正确;改为使用int mainvoid.printft有%edges。\n,num_of_edges;有一个错误的格式说明符,应该是%d。哦,我的天哪…错过了它太可怕了。我想大多数严格设置的编译器都会发现这一点。graph是get_Adjacence中的一个局部变量,但我返回了它,所以如果您愿意,它会保存在main中的graph变量中。无论如何,我的问题不在于图形,而在于边的数量
printf("there are % edges.\n",num_of_edges);