#include<stdio.h>
#include<stdlib.h>
typedef struct
{
double **mat;
int m;
int n;
}Matrix;
void InitialMatrix(Matrix *T,int m,int n)
{
int i;
T->mat=(double**)malloc(m*sizeof(double*));
for(i=0;i<m;i++)
T->mat[i]=(double*)malloc(n*sizeof(double));
T->m=m; // m行
T->n=n; // n列
}
void FreeMatrix(Matrix *T)
{
int i;
for(i=0;i<(T->m);i++)
free(T->mat[i]);
free(T->mat);
}
void SetMatrix(Matrix *T,int m,int n)
{
int i,j;
for(i=0;i<(T->m);i++)
{
for(j=0;j<(T->n);j++)
T->mat[i][j]=i+j;
}
}
void PrintMatrix(Matrix *T)
{
int i,j;
for(i=0;i<(T->m);i++)
{
for(j=0;j<(T->n);j++)
printf(" %3.0f",T->mat[i][j]);
printf("\n");
}
}
int main()
{
Matrix p;
int a,b;
printf("Please enter the matrix row and column:");
scanf("%d%d",&a,&b);
InitialMatrix(&p,a,b);
SetMatrix(&p,a,b);
PrintMatrix(&p);
FreeMatrix(&p);
return 0;
}
用C语言分配动态二维数组(结构体实现)
最新推荐文章于 2024-06-12 14:31:07 发布