gpt4 book ai didi

c - Sscanf 转换为指针,并返回

转载 作者:行者123 更新时间:2023-11-30 19:14:24 24 4
gpt4 key购买 nike

所以我有一个指针(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;
}

最佳答案

我建议这样

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

关于c - Sscanf 转换为指针,并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34122201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com