我编写了一段代码,从用户那里获取二维矩阵的元素,然后使用矩阵的基地址进行打印。 为简单起见,我省略了输入部分。 这是我试过的代码:-
main()
{
int mat[50][50],i, j, r, c, *p;
p = &mat[0][0];
<------skip the input---->
for(i = 0; i < r; i++) /* print the matrix*/
{
for(j = 0; j < c; j++)
{
printf("%d\t", *((p + i) + j));
}
printf("\n");
}
}
Input:
1
2
3
4
Output:
1 2
2 0
看来我的代码是错误的。请帮助。真的很困惑!
最佳答案
基本上您要打印的是 p[i][j]
,它等同于 *(p + (i*rowNum) + j)。这可以如下所示。
printf("%d\t", *(p + (c * i) + j);
关于c - 如何使用基地址在 C 中打印二维矩阵的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42734680/