大佬帮我看看标记地方的问题
程序代码:
#include "stdio.h" #include "stdlib.h" #include "ctype.h" #define capacity 2 int main(void) { double *pstr = NULL; double *value = NULL; double *ave = NULL; int count = 0; int add = 0; char answer = 'n'; do { if (count == add) { add += capacity; pstr = (double*)malloc((add)*sizeof(double)); if (value != NULL) { for (int i = 0; i < count; i++) *(pstr + i) = *(value + i); free(value); } value = pstr; pstr = NULL;有这一行就会运行出错,但是删除这一句就好了,原因是什么? } printf("请输入任意浮点数:\n"); scanf_s(" %lf", pstr + count++); printf("还有吗?\n"); scanf_s(" %c", &answer); } while (tolower(answer) == 'y'); ave = (double*)malloc(count*sizeof(double)); if (count < add) { for (int i = count; i < add; i++) free(value + i); } for (int i = 0; i < count-1; i++) { *ave = 0.0; *ave += *(value + i); } printf("%.2lf\n", *ave / count); free(ave); free(value); return 0; }