这张图片有什么问题?

huangapple go评论44阅读模式
英文:

What is wrong with this picture?

问题

这是程序 链接

所以,我知道如何在C中声明变量。我知道double*表示它只是一个指针。但我没有将它声明为指针。这在我的笔记本电脑和台式电脑上都发生了,突然之间,我写的任何东西都不起作用,我不明白发生了什么变化。

英文:

[This is the program] https://i.stack.imgur.com/kcoAO.png)

So, I know how to declare a variable in c. I know that double* means it is only a pointer. But I didn't declare it as a pointer. This is happening on both my laptop and pc, all of a sudden, nothing I'm writing is working and I don't understand what changed.

答案1

得分: 4

第16行,即printf()函数的语法是输出内容时不使用指针。

printf("Something %lf", &x) 这里的 &x 意味着变量x的地址,而不是x的值。

只需使用 printf("Something %lf", x) 这样输出x的值。

现在,为什么在scanf中要使用地址呢?因为它是这样写的。scanf("%d", &x) 意味着读取一个整数并将其存储在变量 x 的地址中。但在使用 printf 时,我们不需要使用变量地址来显示值。

你可以用另一种方式显示值,printf("Something %lf", *(&x))

& 运算符用于确定变量的地址,* 运算符用于获取位置的值。因此,表达式 *(&x) 归结为存储在变量 x 的地址处的值,与 x 的值相同。

英文:

On line number 16, i.e. the printf() functions...the syntax for a printf function to output something is not to use pointer.

printf("Something %lf", &x) here &x means address of the variable x and not the value of x.

You simply need to use printf("Something %lf", x) this outputs the value of x.

Now, why address of is used in scanf is because it says so. scanf("%d", &x) means that read an integer and store it in the address of variable x. But while using printf we need not use the address of variable to display the value.

Another way you can display the value is printf("Something %lf", *(&x))

The & operator is used for determining the address of a variable and * operator is used as value at location. Thus the expressiong *(&x) boils down to the value stored at the address of varuable x which is same as the value of x.

huangapple
  • 本文由 发表于 2023年4月17日 08:51:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定