gpt4 book ai didi

c - 在c中使用指针和函数显示二维数组

转载 作者:行者123 更新时间:2023-12-01 22:28:49 25 4
gpt4 key购买 nike

<分区>

尊敬的stackoverflow成员(member), 是 c 程序的完整电枢,我想使用指针访问矩阵的元素。就像我想使用点打印矩阵的元素一样。我附上了代码以及不正确的输出。请帮助我。谢谢

`
#include<stdio.h>
#include<conio.h>

void print(int **arr, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ",*(arr+i*n+j)); //print the elements of the matrix
//printf("%d ", *((arr+i*n) + j));
}
printf("\n");
}
}


int main()
{
int arr[20][20],m,n,c,d;
clrscr();
printf("row=\n");
scanf("%d",&m);
printf("col=\n");
scanf("%d",&n);
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{
scanf("%d",&arr[c][d]);
}
}
for(c=0;c<m;c++) //print the matrix without function calling
{
for(d=0;d<n;d++)
{
printf("%d ",arr[c][d]);
}
printf("\n");
}
print((int **)arr, m, n); //print the matrix using function calling
getch();
return 0;
}`

上面的代码产生如下所示的输出

row=
2
col=
3
//elements of the matrix
2
3
4
5
6
7
//print without using function
2 3 4
5 6 7
//print using function"print(int **a,int m,int n)"
2 3 4
0 0 0

当使用函数时,我没有得到准确的矩阵值。注意:应使用 print(int **a,int m,int n)。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com