一维数组方法:
int* getArray(int n) { int i, j; int* a = (int*)malloc(n * sizeof(int)); //申请内存空间,大小为n个int长度。 printf("请输入一维数组:"); for ( i = 0; i < n; i++) { scanf_s("%d", &a[i]); } return a; }
二维数组方法:
int** calTwoArray() { int i, j; int** b; b = (int**)malloc(2 * sizeof(int*)); //先申请M个指针型字节的空间 for ( i = 0; i < 2; i++) { b[i] = (int*)malloc(3 * sizeof(int)); //然后依次按一维申请 } //int a[2][3]; printf("请输二维数组:"); for ( i = 0; i < 2; i++) { for ( j = 0; j <3; j++) { scanf_s("%d", &b[i][j]); } } return b; }
主main方法:
int main() { int i, j; //一维数组打印 int* a = getArray(5); for (i = 0; i < 5; i++) { printf("%d\n", a[i]); } //二维数组打印 int** b = calTwoArray(); for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { printf("%d\n", b[i][j]); } } return 0; }
标签:一维,int,C语言,++,二维,数组,printf From: https://www.cnblogs.com/yunchen/p/17055349.html