简体   繁体   中英

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. As soon as you get out of read () function, that pointer is lost.

When you send p1 as an **int, the compilers sends the value of the 1st pointer by copy (here unitialized), not the adress. 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.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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