标签:
一维动态数组的创建,这个比较简单,直接上代码
1 #define _CRT_SECURE_NO_DEPRECATE 2 #include<stdio.h> 3 #include<stdlib.h> 4 void createOneDimensionalVector(){ 5 int n, i; 6 int *arr; 7 scanf("%d",&n); 8 arr = (int*)malloc(sizeof(int)*n); 9 for (i = 0; i < n; i++) 10 arr[i] = i; 11 for (i = 0; i < n; i++) 12 printf("%d\t",arr[i]); 13 } 14 15 int main(){ 16 createOneDimensionalVector(); 17 18 return 0; 19 }
二维动态数组的创建
1 void createTwoDimensionalVector(){ 2 int n=3, m=4; 3 int** a; 4 a = (int**)malloc(sizeof(int*)*n);//创建一个指针数组,把指针数组的地址赋值给a 5 for (int i = 0; i < n; i++) 6 a[i] = (int*)malloc(sizeof(int)*m);//给第二维分配空间 7 8 for (int i = 0; i < n; i++){ 9 for (int j = 0; j < m; j++) 10 a[i][j] = i + j; 11 } 12 13 for (int i = 0; i < n; i++){ 14 for (int j = 0; j < m; j++) 15 printf("%d\t",a[i][j]); 16 printf("\n"); 17 } 18 } 19 int main(){ 20 createTwoDimensionalVector(); 21 22 return 0; 23 }
标签:
原文地址:http://www.cnblogs.com/cplinux/p/5768290.html