C語言中malloc 動態申請多維數組

#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);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章