新手求助大神!关于动态二维数组.
我想定义一个动态二维数组elem[i][j]先输入i
知道i之后再用 for 循环 i++ 一个一个输入 j
怎么写才行.?
求助啊..
#include<stdio.h> int ** getMatrix(int rows, int cols) { int i; int **p; p = (int **)malloc(rows * sizeof(int *)); for(i = 0; i < rows; i++) p[i] = (int *)malloc(cols * sizeof(int)); return p; } void deleteMatrix(int **p, int rows) { int i; for(i = 0; i < rows; i++) free(p[i]); free(p); } int main() { int **p, i, j, rows, cols; rows = 5; cols = 5; p = getMatrix(rows, cols); for(i = 0; i < rows; i++) for(j = 0; j < cols; j++) p[i][j] = i * rows + j; for(i = 0; i < rows; i++) { for(j = 0; j < cols; j++) printf("%5d", p[i][j]); printf("\n"); } deleteMatrix(p, rows); return 0; }