简体   繁体   English

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

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

I'm unable to print the matrix that i build using the dynamic memory allocation in read function. 我无法打印使用读取功能中的动态内存分配构建的矩阵。 Please guide me to pass the values from read to disp function. 请指导我将值从读取传递到显示功能。

I've tried passing pointer to pointer in single pointers but i have no idea about double pointers please help me. 我尝试过将指针传递给单指针中的指针,但是我不知道双指针,请帮帮我。

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);
}

The expected output should be the sparse matrix to be printed but it refuses to do so after scanning the elements. 预期的输出应该是要打印的稀疏矩阵,但是在扫描元素之后拒绝这样做。

I have made several changes to your program. 我对您的程序进行了几处更改。 See my comments in the code. 请参阅代码中的注释。

#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);
}

You allocate a memory pointer in the read () function for the p1 2D array. 您可以在read()函数中为p1 2D数组分配一个内存指针。 As soon as you get out of read () function, that pointer is lost. 一旦退出read()函数,该指针就会丢失。

When you send p1 as an **int, the compilers sends the value of the 1st pointer by copy (here unitialized), not the adress. 当将p1作为** int发送时,编译器将按副本(此处为单位化)而不是地址发送第一个指针的 So if you change it in the function, you only modify a copy of this pointer, not the pointer itself. 因此,如果在函数中对其进行更改,则仅修改该指针的副本,而不修改指针本身。

A Quick solution would be to send &p1, receive a ***int in your read () function, and use P1 with a '*' each time (*p1)[i][j] for instance. 一种快速的解决方案是发送&p1,在您的read()函数中接收*** int,并在每次(* p1)[i] [j]时都将P1与'*'一起使用。

BUT that's only a quick & dirty fix for a code that hasn't been designed properly. 但是 ,这只是针对代码设计不当的快速而肮脏的修复程序。 You could have your 2D array embedded in a struc for instance... that would be cleaner already :) 例如,您可以将2D数组嵌入到struc中……这已经更加干净了:)

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

相关问题 我在用read()和write()做错了什么? - What am I doing wrong with read() and write()? 我的c函数在做什么错 - What am i doing wrong in my c function 此功能使.exe崩溃,我在做什么错? - This function makes the .exe crash, what am I doing wrong? 我在用此功能做错什么,它并没有结束 - What I am doing wrong with this function, it does not end 使用结构将链表传递给 function 我做错了什么? - Pass linked list to function with struct what I doing wrong? 在 function 的结构中修改指针的正确方法是什么?我做错了怎么办? - What is the right way for modifying a pointer within a struct in a function?Am I doing it wrong How to do it? 我在这里做错了什么? 无论我做什么,function askSubscriptionQuestions 都不会存储 4 个浮点数的值 - What am I doing wrong here? No matter what I do, the function askSubscriptionQuestions does not store the values of the 4 floats 将 function 传递给 C 中的另一个 function。 我究竟做错了什么? - Passing a function to another function in C. What am I doing wrong? 使用系数的动态分配数组评估多项式,我在做什么错? - Evaluating a polynomial, using a dynamically allocated array for coefficients, what am I doing wrong? 我在交换病人姓名的“ trier”功能中做的错误吗? - Is it wrong what I am doing in function “trier” which swaps the name of patient?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM