我究竟做错了什么?如何将读取函数中分配的内存传递给显示函数?

What am i doing wrong? How can i pass the memory allocated in read function to disp function?

我无法打印使用读取函数中的动态内存分配构建的矩阵。请指导我将值从 read 传递给 disp 函数。

我试过在单指针中将指针传递给指针,但我不知道双指针,请帮助我。

int i; //global
struct pass{
    int row,col,ele; 
} a1;

void disp(int** , struct pass);
void read(int** , struct pass);

void disp(int** p, struct pass a)
{
    printf("the triplet representation is\n"); //program stops here everytime
    for(i=0;i<=a.ele;i++){
        if(i==0){
            printf("row\t column\t element\n");
            printf("%d\t %d\t %d\n", p[i][0], p[i][1], p[i][2] );
        }
        else
            printf("%d\t %d\t %d\n", p[i][0], p[i][1], p[i][2] );
    }
}

void read(int** p, struct pass a)
{
    int i;
    printf("enter no. rows, columns, elements\n");
    scanf("%d %d %d", &a.row, &a.col, &a.ele);

    p=(int* *)malloc(sizeof(int*)*(a.ele+1));

    for(i=0;i<=a.ele;i++)
    p[i]=(int *)malloc(3*sizeof(int));

    p[0][0]=a.row; p[0][1]=a.col; p[0][2]=a.ele;

    printf("enter rows, columns, and elements\n");
    for(i=1;i<=a.ele;i++){
        scanf("%d %d %d", &p[i][0], &p[i][1], &p[i][2]);
    }
}

int main()
{
    int **p1;
    read(p1, a1);
    disp(p1,a1);
}

预期的输出应该是要打印的稀疏矩阵,但它在扫描元素后拒绝这样做。

你在read()函数中为p1二维数组分配了一个内存指针。一旦你退出 read() 函数,那个指针就丢失了。

当您将 p1 作为 **int 发送时,编译器通过副本(此处已初始化)而不是地址发送第一个指针的 value。所以如果你在函数中更改它,你只是修改了这个指针的副本,而不是指针本身。

一个快速的解决方案是发送 &p1,在你的 read () 函数中接收一个 ***int,然后每次使用带有 '*' 的 P1 (*p1)[i][j] 例如。

BUT 对于未正确设计的代码,这只是一种快速而肮脏的修复。例如,您可以将 2D 数组嵌入到 struc 中……这样已经更干净了:)

我对你的程序做了几处修改。请参阅我在代码中的评论。

#include "stdio.h"
#include "stdlib.h"

/* It is better to declare counter variable like i locally. */
/*int i; //global*/

/* This struct is not really needed since its contents is part of the sparse matrix. */
/*struct pass {
    int row, col, ele;
} a1;*/

void disp(int ** p)
{
    printf("the triplet representation is\n"); //program stops here everytime
    for (int i = 0; i <= p[0][2]; i++) {
        if (i == 0) {
            printf("row\t column\t element\n");
            printf("%d\t %d\t %d\n", p[i][0], p[i][1], p[i][2]);
        }
        else
            printf("%d\t %d\t %d\n", p[i][0], p[i][1], p[i][2]);
    }
}

/* Reads sparse matrix and returns int ** pointer to it.
   m[0] -> #rows, #cols, #elements
   m[1] -> row index, col index, value
   m[2] -> etc.
   m[#eleents] -> ...

   By returning pointer to sparse matrix, it makes code easier
   to understand than passing a pointer to the sparse matrix,
   that is int ***.
*/
int** read()
{
    int i;
    printf("enter no. rows, columns, elements\n");
    int row, col, ele;;
    scanf("%d %d %d", &row, &col, &ele);

    int ** p = (int**)malloc(sizeof(int*)*(ele + 1));

    for (i = 0; i <= ele; i++)
        p[i] = (int *)malloc(3 * sizeof(int));

    p[0][0] = row; p[0][1] = col; p[0][2] = ele;

    printf("enter rows, columns, and elements\n");
    for (i = 1; i <= ele; i++) {
        scanf("%d %d %d", &p[i][0], &p[i][1], &p[i][2]);
    }
    return p;
}

int main()
{
    int **p1 = read();
    disp(p1);
}