【发布时间】:2014-05-08 14:04:46
【问题描述】:
我在从 while 循环全局更新数组时遇到问题,如下所述。请注意,我只能使用 C 95 及之前的功能。任何帮助将不胜感激!满粘贴箱http://pastebin.com/ss6VgTCD
在我的程序顶部声明
int data_count, i;
float *x_values, *y_values;
float x[100],y[100];
在我的主函数中,我的数组是使用以下代码创建的:
printf("\nPlease Enter How Many Data Points You Wish To Enter: \n");
scanf("%d", &data_count);
x_values=(float*)calloc(data_count,sizeof(*x_values));
y_values=(float*)calloc(data_count,sizeof(*y_values));
if (x_values==NULL) {
printf("Error! Memory Could Not Be Allocated. ");
exit(0);
}
文件读取功能用于导入以前输入的数据,该功能正在获取正确的数据并在我的调试行printf("%12f%12f\n", x_values[i], y_values[i]);中显示正确的数据点但是仅在本地更新x_values和y_values,因为这些导入的数据不能被程序的其余部分看到。如何全局更新数组?
void file_read(void) {
store = fopen ("j:/StoredValues.txt", "r");
if (store == NULL )
printf("\nError: Failed To Open Previous Data File - Program Will Continue Anyway\n");
else {
printf("\nSuccess: Data From Previous Run Imported\n");
i=0;
do {
fscanf ( store, "%f,%f\n", &x[i], &y[i]);
x_values = x;
y_values = y;
printf("%12f%12f\n", x_values[i], y_values[i]);
i=i+1;
} while (!feof(store));
fclose(store);
}
}
附言我只用 C 编写了 2 周,所以很简单很好:)
【问题讨论】:
-
没有什么叫 C95,假设你的意思是 1995 年的 C90+AM1。在 C90+AM1 中强制转换 calloc 的结果是危险的,所以不要这样做。在 C99 中不再危险,只需 pointless。
标签: c arrays malloc dynamic-arrays calloc