The creation of a one-dimensional dynamic array, which is relatively simple, directly on the code
1 #define_crt_secure_no_deprecate2#include <stdio.h>3#include <stdlib.h>4 voidCreateonedimensionalvector () {5 intn, I;6 int*arr;7scanf"%d",&n);8Arr = (int*)malloc(sizeof(int)*n);9 for(i =0; I < n; i++)TenArr[i] =i; One for(i =0; I < n; i++) Aprintf"%d\t", Arr[i]); - } - the intMain () { - createonedimensionalvector (); - - return 0; +}
Creation of two-dimensional dynamic arrays
1 voidCreatetwodimensionalvector () {2 intn=3, m=4;3 int**A;4A = (int**)malloc(sizeof(int*) *n);//create an array of pointers to assign the address of the array of pointers to a5 for(inti =0; I < n; i++)6A[i] = (int*)malloc(sizeof(int) *m);//allocating space to the second dimension7 8 for(inti =0; I < n; i++){9 for(intj =0; J < M; J + +)TenA[I][J] = i +J; One } A - for(inti =0; I < n; i++){ - for(intj =0; J < M; J + +) theprintf"%d\t", A[i][j]); -printf"\ n"); - } - } + intMain () { - createtwodimensionalvector (); + A return 0; at}
The establishment of dynamic array in C language