我可以使用*p
打印printf("%d",p[1])
,但我无法使用**p
打印printf("%d",p[1][1]
。
我试着编写一个分配内存并将其地址分配给struct的元素(**成员)的函数。但我仍然无法打印出来。
#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;
}