gpt4 book ai didi

c - scanf 没有正确保存值

转载 作者:太空宇宙 更新时间:2023-11-04 01:45:29 25 4
gpt4 key购买 nike

在我定义的函数中使用 scanf 来更改我通过引用该函数传递的数组中的值时,它根本不会更改值,同时是 double 类型。

当我更改占位符时:scanf("%1f", &x)到scanf("%d", &x)它按预期工作,但我需要为这个项目使用 double 。

int main()
{
double varArray[MAX];
zeroFill(varArray);
}


void fill(double *array) {
for(int i =0; i<MAX; i++){
printf("what is index %d?\n", i);
scanf("%f",(array+i));
printf("%f \n", *(array+i));
}

不是更改该索引处的值,而是将其更改为 0.000000 或一个奇怪的数字:

 what is index 0?
1
0.000000
what is index 1?
2
0.000000
what is index 2?
3
-0.000000
what is index 3?
4
0.000000
what is index 4?
5
0.000000
what is index 5?
6
0.000000
what is index 6?
7

18204394847704181818181818181818181818181810000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000来了

因此对于输入 3 和 7,它返回了一个 -0.000000 和一个 v 大数字。然而,这并不一致,因此其他输入会给出类似的输出

最佳答案

要将 scanf 的结果写入 double,请使用 %lf%f 用于float

不要将 %ddouble 一起使用。 %d 用于int

(打印时,%lf%f 可用于 floatdouble . 这是因为在调用 printf 时,或者在调用参数对应于 ... 在参数或任何没有原型(prototype)声明的函数中。)

关于c - scanf 没有正确保存值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54608477/

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