指针浮动阵列的第一个机会例外
First chance exception with pointer floating array
int main(int argc, char** argv)
{
printf("Enter the file name:n");
char inputFileLoc[100], outFileLoc[100];
scanf("%s", inputFileLoc);
int * n = 0;
float rcoef[2];
FILE * inFile = fopen("D:\test.txt", "r");
FILE * outFile = fopen(outFileLoc, "w");
if (inFile == NULL)
{
printf("File not found at %s", inputFileLoc);
}
else
{
printf("How many data points should we read in?");
scanf("%i", &n);
float *xdata = (float *)calloc(sizeof(float), *n);
float *ydata = (float *)calloc(sizeof(float), *n);
for (int i = 0; i < *n; i++)
{
fscanf(inFile, "%f%f", &xdata[i], &ydata[i]);
}
fregression(xdata, ydata, rcoef);
printf("Where would you like to save the file to?n");
scanf("%s", outFileLoc);
fprintf(outFile, "The slope and intercept are %f and %f", rcoef[0], rcoef[1]);
free(xdata);
free(ydata);
}
fclose(inFile);
fclose(outFile);
return 0;
}
我在" float *xdata =(float *)....的。
这是不正确的:
int * n = 0;
应该更改为
int n = 0;
scanf
应该获取要写入的地址,而不是指针的地址。即使您想使用指针,也应分配内存,n
也应指向这一点。在这种情况下,传递到scanf
时,您应该将&
丢给n
,即scanf("%i", n);
应该很好。
-
将 int *n 更改为 int n 当您试图写入未初始化的内存时。
-
在calloc中也进行了更改,因为它试图从 *n分配,该更改未正确定义
尝试进行两个更改时,它可以正常工作。即使在此之后您会遇到错误。
相关文章:
- 将一个阵列中的数据存储到另外两个阵列中不起作用
- 特征:创建一个具有函数的三维阵列
- 将积分类型的数组作为另一个不相关的积分类型的阵列进行访问的安全且符合标准的方法
- 围绕二维阵列做一个圆圈
- 为什么一个二维阵列会导致赛段故障,而另一个则不会导致?
- 如何将X 2维数组连接到一个三维阵列中
- 如何打印一个字符阵列和其中的特定字符
- 从一个文件读取到三个并行阵列
- 内存C++复制是否将内存地址复制到另一个阵列
- 如何在堆上分配一个void*阵列
- C++将 2D 阵列的一部分复制到另一个 2D 阵列,性能
- 一个NxM阵列或大小为M的N个阵列
- C 按顺序排列一个指针阵列
- 将分配给一个3D阵列
- 从QTScript操纵一个2D阵列
- 算法将一个硬币矩阵转移到另一个硬币阵列
- 创建一个3x3x3阵列.用0到9之间的随机值填充.输出数组.找出3个最小值和3个最大值的输出
- 用C++将一个字符数组中的四个元素读入另一个字符阵列
- 哪个更好:一个unique_ptr到一个2D阵列,还是一个unique _ptr的2D阵列
- 将2d阵列的实际值复制到另一个2d阵列c++