C语言中malloc 动态申请多维数组

来源:互联网 发布:中国移动m823软件 编辑:程序博客网 时间:2024/06/06 08:36
#include "stdio.h"#include "stdlib.h"void main(){int **a;int n;printf("please enter the number of the matrix:");scanf("%d",&n);printf("please input the %d*%d matrix:\n",n,n);a=(int**)malloc(n*sizeof(int*));for(int i=0;i<n;i++){a[i] = (int*)malloc(n*sizeof(int));for(int j=0;j<n;j++){scanf("%d",a[i]+j);}}printf("the reversed matrix is:\n");for(int m=0;m<n;m++){for(int k=0;k<n;k++){printf("%d ",a[k][m]);}printf("\n");}free(a);}


0 0
原创粉丝点击