printf为什么会改变函数指针的地址?

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

Why does printf changes the address of a function pointer?

问题

The address operator & in C is used to obtain the memory address of a variable or function. In your code examples, you are using it with a function pointer fp. Here's the translation of your code and the explanation:

#include <stdio.h>
int main()
{
    int (*fp) (int);
    printf("%p\n", (fp));
    printf("%p", &(fp));
}

When you run this code, you get the following output:

0x560cb44ce060
0x560cb44ce060

In this case, you are printing the address of the function pointer fp twice, and both addresses are the same because you are not changing the address of fp.

Now, let's look at the second code snippet:

#include <stdio.h>
int main()
{
    int (*fp) (int);
    printf("%p\n", (fp));
    printf("%p", &(fp));
}

When you run this code, you get the following output:

0x1000
0x7ffcc92c8990

In this case, you added the & operator before (fp) in the second printf. This means you are now printing the address of the function pointer fp and the address of the pointer variable fp. The difference in the addresses is because fp is an uninitialized pointer, so its value is indeterminate. The & operator applied to fp gives you the address of the pointer variable itself, which can be different from the address it's pointing to (0x1000).

So, adding the & operator changes what you are printing. The first printf without & prints the address stored in fp, while the second printf with & prints the address of the pointer variable fp.

英文:

Adressing operator inside the printf changes the address of the function pointer from a regular adress to 0x1000. Why this happens and what does it mean ?

#include &lt;stdio.h&gt;
int main()
{
	int (*fp) (int);
	printf(&quot;%p\n&quot;, (fp));
	printf(&quot;%p&quot;, (fp));
}

When I run this I get
0x560cb44ce060
0x560cb44ce060

as expected but when ı run this

#include &lt;stdio.h&gt;
int main()
{
	int (*fp) (int);
	printf(&quot;%p\n&quot;, (fp));
	printf(&quot;%p&quot;, &amp;(fp));
}

ı get
0x1000
0x7ffcc92c8990

I really can not figured out what does change when I add &amp; operator at the last line.

答案1

得分: 4

在第一个使用 printf 的情况下,输出了未初始化的指针 fp(它的垃圾值)

int (*fp) (int);
printf("%p\n", (fp));

在第二个使用 printf 的情况下,输出了指针 fp 本身的有效地址

printf("%p", &(fp));

所以没有任何变化。输出了两个不同的实体:存储在变量(指针)中的值和变量本身的地址。

请注意,转换说明符 p 期望的是 void * 类型的指针,但函数指针可能无法转换为 void * 类型。

英文:

In the first case of using printf there is outputted the uninitialized pointer fp (its garbage value)

int (*fp) (int);
printf(&quot;%p\n&quot;, (fp));`

In the second case of using printf there is outputted the valid address of the pointer fp itself

printf(&quot;%p&quot;, &amp;(fp)); 

So there is nothing changed. There are outputted two different entities: the value stored in a variable (pointer) and the address of the variable itself.

Pay attention to that the conversion specifier p expects a pointer of the type void * but function pointers may not be converted to the type void *.

答案2

得分: 3

也许您混淆了指针所指向的地址和指针的地址。

第一个%p将输出fp保存的地址(尽管它是垃圾值,因为您没有初始化它),而第二个%p将输出fp的地址。

英文:

Maybe you confonded the address which pointer is pointing to and the pointer's address.

The first %p will output the address fp saved(though it is garbage value because you didn't initialized it), and the second one will output the fp's address.

huangapple
  • 本文由 发表于 2023年5月10日 16:44:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216491.html
匿名

发表评论

匿名网友

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

确定