[Go to] C language to build a complete dynamic array instance, dynamic array
[Switch] C language to build a complete dynamic array instance
Address: http://www.jb51.net/article/52153.htm
This article uses a complete example code to briefly describe how to build a dynamic array in C language for your reference. The complete example is as follows:
?
123456789101112131415161718 |
#include <stdio.h> #include <malloc.h> int main( void ) { int len; int * arr; printf ( "Enter the length of the array :" ); scanf ( "%d" , &len); arr = ( int *) malloc ( sizeof ( int )*len); printf ( "Enter the value of the array :" ); for ( int i = 0; i < len; i ++) { scanf ( "%d" , &arr[i]); } for ( int j = 0; j < len; j ++) { printf ( "%d:%d " , j , arr[j]); } free (arr); return 0; } |
The running result is as follows:
?
12345 |
E:\clearning\cpointer>gcc dynamicarray.c -o dm --std=c99 E:\clearning\cpointer>dm Enter the length of the array: 5 Enter the value of the array: 1 2 3 4 5 0:1 1:2 2:3 3:4 4:5 |