C builds a full instance of a dynamic array

  • 2020-04-02 02:27:05
  • OfStack

In this paper, a complete example code to brief the C language to build dynamic array method, for your reference, the full example is as follows:


#include <stdio.h>
#include <malloc.h>
int main(void) {
    int len;
    int * arr;
    printf(" Please enter the array length :");
    scanf("%d", &len);
    arr = (int *)malloc(sizeof(int)*len);
    printf(" Please 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 operation results are as follows:


E:clearningcpointer>gcc dynamicarray.c -o dm --std=c99
E:clearningcpointer>dm
 Please enter the array length :5
 Please enter the value of the array :1 2 3 4 5
0:1 1:2 2:3 3:4 4:5

Related articles: