Sscanf成指针,带回归

时间:2015-12-06 20:16:40

标签: c

所以我有一个指针(b),我想读取指针中的数字,我无法解决它:\

double* 
scanner(double* b)
{
    while ((fgets(line, sizeof line, stdin) != NULL) && (line[0] != '\n'))
    {
        b =(double*)malloc(sizeof(double)*n);
        if (sscanf(line, "%lf\n", b[n]) == 1)
        {
            printf("%ld",b[n]);
        }

        n++;
    }

    return b;
}

1 个答案:

答案 0 :(得分:1)

我建议喜欢这个

double *bekerdezo2(size_t *n){
    double *b = NULL, value;
    *n = 0;

    while ((fgets(line, sizeof line, stdin) != NULL) && (line[0] != '\n')){
        if (sscanf(line, "%lf", &value) == 1){
            b = realloc(b, sizeof(*b) * (*n + 1));
            b[(*n)++] = value;
        } else {
            break;
        }
    }
    return b;
}

DEMO