c - 代码构建成功但崩溃了
问题描述
我可以用 打印*p
,printf("%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;
}
解决方案
推荐阅读
- sql - SQLQuery 不返回查询的文档
- python - 按键盘上的 Q 键无法完成游戏
- javascript - 出于 SEO 原因,Django 模板 + Vue 模板用于相同的内容
- excel - 如何修改代码以粘贴到下一个空白行而不是列?
- java - android studio中的私有字段
- twilio-flex - 将 Twilio Flex 嵌入到自定义应用程序中
- python - 在 50 个变量 x 100k 行数据集上优化 K-最近邻算法
- android - 作为开发人员,我有什么办法可以防止电话夜间模式设置强行关闭我的应用程序?
- python - 如何获得元组和元组列表之间的最近距离?
- apache-beam - 无法对 GroupByKey 转换输出进行多次迭代