我被困在一个问题上有一段时间了,我需要从表单中读取用户的输入,
5 1.2 2.3 3.4 4.5 5.6其中第一个整数是期望的浮点数,接下来的浮点数是我需要存储在这个大小的数组中的值。我一直在返回错误的代码是
...
int i = 0, j, k;
float value, *ptr;
// For every element in inputArr...
while (i < inputLength) {
printf("Enter the number of values in this data set, followed by the values: ");
// Get the int value for array creation...
scanf("%d ", &j);
printf("%d", j);
// Save it for the calculations later.
*(lengths + i) = j;
// Create dynamic array of floats.
*(inputArr + i) = calloc(j, sizeof(float));
ptr = *(inputArr + i);
// For the rest of the input read the floats and place them.
k = 0;
while (k < j-1) {
scanf("%f ", &value);
*(ptr + k) = value;
k++;
}
scanf("%f\n", &value);
*(ptr + j - 1) = value;
i++;
}当我输入上面的输入时,这会引发分段错误。有人能告诉我我做错了什么吗?
发布于 2014-09-28 19:41:18
您不必在扫描调用中包含空格和结束字符串。
scanf("%d", &j);i/o
scanf("%d ", &j);
scanf("%f", &value);i/o
scanf("%f\n", &value);等。
https://stackoverflow.com/questions/26088854
复制相似问题