首页 > 解决方案 > 代码构建成功但崩溃了

问题描述

我可以用 打印*pprintf("%d",p[1])但不能**p用 打印printf("%d",p[1][1]。我试图编写一个分配内存并将其地址分配给结构的元素(**成员)的函数。但我仍然无法打印它。

#include<stdio.h>
#include<stdlib.h>

typedef struct 
{
    int **members;
    int line; //number of lines
    int clmn; //number of columns
}matrix;

void mamal(matrix matrix1) //function to allocate memory  
{

    int ** m; 
    m=(int**)calloc(matrix1.clmn,sizeof(int*));
    int i;
    for(i=0;i<(matrix1.clmn);i++)
    {
        m[i]=(int*)calloc(matrix1.line,sizeof(int));
    }
    matrix1.members=m; // allocated memroy assigned to struct elemnt
}

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

matrix matrx1;

printf("Enter Column:");        //Reading struct elements
scanf("%d",&matrx1.clmn);       //Reading struct elements

printf("Enter Line:");          //Reading struct elements
scanf("%d",&matrx1.line);       //Reading struct elements

mamal(matrx1);  
int i,j;
for(i=0;i<matrx1.clnm;i++)
{
    for(j=0;j<matrx1.line;j++)
    {
        printf("[%d][%d]:%d",(i+1),(j+1),matrx1.members[i][j]); //This line causes crash I think. I can't print memory block that **pointer points.
    }
}

    return 0;
}

标签: c

解决方案


推荐阅读