//使用malloc()函数实现动态数组
#include<stdio.h> #include<stdlib.h> int main(void) { double * ptd; int max; int number; int i = 0; puts("what is the maxinum number of type double entries?\n"); scanf("%d",&max); ptd = (double *)malloc(max * sizeof(double)); if(ptd == NULL) { puts("Memory allocation is failed."); exit(0); } puts("enter the values:\n"); while(i < max && scanf("%lf",&ptd[i]) == 1) ++i; printf("here are you %d entries:\n",number = i); for(i = 0; i < number; i++) printf("%7.2f",ptd[i]); putchar('\n'); free(ptd); return 0; }