C 如何读取长度不确定的数字输入?

C 如何读取长度不确定的数字输入?,c,C,我有一段时间一直在解决一个问题,我需要读取表单中用户的输入 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

我有一段时间一直在解决一个问题,我需要读取表单中用户的输入

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++;
}
。。。
int i=0,j,k;
浮动值*ptr;
//对于inputArr中的每个元素。。。
while(i
当我输入上面的输入时,这会引发分段错误。
有人能告诉我我做错了什么吗

在scanf调用中不必包含空格和字符串结尾

scanf("%d", &j);
输入输出

输入输出


等等。

你没有说你得到了什么错误。@hugomg我现在把它添加到问题中-这是一个分段错误。你知道哪一行给出了分段错误吗?(你可以在调试器下运行代码来找出答案)看起来是我使用指针的方式造成的。当然,分段错误来自指针。问题是,如果你知道是哪条线导致了问题,那么解决问题就容易多了。(你知道如何使用调试器吗?)这修复了我与scanf问题相关的问题,似乎分段错误发生在我如何使用指针上。谢谢
scanf("%d ", &j);

scanf("%f", &value);
scanf("%f\n", &value);