printf()和void * [英] printf() and void *

查看:325
本文介绍了printf()和void *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从前面的讨论中回想一下,在使用printf(%p)打印其值之前,我们必须将一个非void

指针强制转换为void *。是

是真的,如果是的话为什么?

I recall from previous discussions here, that we must cast a non-void
pointer to void * before printing its value with printf("%p"). Is it
true, and if yes why?

推荐答案

Harald van D?| k< true ... @ gmail.comwrote:
Harald van D?|k <true...@gmail.comwrote:

... [该]示例打印函数的表示形式

指针,而不是其值。
... [the] example prints the representation of a function
pointer, not its value.



它的值由表示确定。

It''s value is determined by the representation.


如果存在多个函数指针表示

一个值,没有可移植的方式来打印价值

这样的方式使得不同的表示变得无法区分。
If multiple function pointer representations exist for
one value, there is no portable way to print the value
in such a way that the different representations become
indistinguishable.



这与%p的情况有何不同?唯一可以保证的是,如果你通过%p阅读,你将获得一个与原始数字相等的指针。


''hex dump''方法有相同的保证。


-

彼得

How is this any different to the case for %p? The only
thing you''re guaranteed is that if you read via %p you''ll
get a pointer that compares equal to the original.

The ''hex dump'' approach has the same guarantee.

--
Peter


On Sun,2008年3月2日19:45:29 -0800,Peter Nilsson写道:
On Sun, 02 Mar 2008 19:45:29 -0800, Peter Nilsson wrote:

Harald van D?3k< true ... @ gmail.comwrote:
Harald van D?3k <true...@gmail.comwrote:

> ... [该]示例打印函数指针的表示形式,而不是其值。
>... [the] example prints the representation of a function pointer, not
its value.



它的值由表示决定。


It''s value is determined by the representation.



当然,但对所有类型都是如此。我们是否应该开始打印1.01作为

a系列字节呢?

Of course, but that''s true for all types. Should we start printing 1.01 as
a series of bytes too, then?


>如果有多个功能对于一个值存在指针表示,没有可移植的方式来打印值,使得不同的表示变得难以区分。
>If multiple function pointer representations exist for one value, there
is no portable way to print the value in such a way that the different
representations become indistinguishable.



这与%p的情况有何不同?唯一可以肯定的是,如果你通过%p阅读,你会得到一个指针,比较

等于原始值。


How is this any different to the case for %p? The only thing you''re
guaranteed is that if you read via %p you''ll get a pointer that compares
equal to the original.



如果你将printf的相同值打印两次,你可以确保两次获得

相同的字符串。如果你打印两个相同值的表示

两次,你不能确定你得到两次相同的字符串。

If you print the same value to printf twice, you can be sure you get the
same string twice. If you print the representation of two identical values
twice, you can''t be sure you get the same string twice.


3月3,上午12:19,Harald van D?| k< true ... @ gmail.comwrote:
On Mar 3, 12:19 am, Harald van D?|k <true...@gmail.comwrote:

On Sun,2008年3月2日19:45: 29 -0800,Peter Nilsson写道:
On Sun, 02 Mar 2008 19:45:29 -0800, Peter Nilsson wrote:

Harald van D?| k< true ... @ gmail.comwrote:
Harald van D?|k <true...@gmail.comwrote:

... [该]示例打印函数指针的表示形式,而不是其值为b

... [the] example prints the representation of a function pointer, not
its value.


它的值由表示决定。
It''s value is determined by the representation.



当然,但对所有类型都是如此。我们是否应该开始将1.01打印为

一系列字节呢?


Of course, but that''s true for all types. Should we start printing 1.01 as
a series of bytes too, then?



这不是什么二进制文件格式是关于?

Isn''t it what "binary file formats" is about?


如果一个值存在多个函数指针表示,那么

不是一种可移植的方式来打印价值,使得不同的

表示变得无法区分。
If multiple function pointer representations exist for one value, there
is no portable way to print the value in such a way that the different
representations become indistinguishable.


这与%p的情况有何不同?唯一可以肯定的是,如果你通过%p阅读,你会得到一个指针,比较

等于原始值。
How is this any different to the case for %p? The only thing you''re
guaranteed is that if you read via %p you''ll get a pointer that compares
equal to the original.



如果你对printf打印两次相同的值,你可以确保两次获得

相同的字符串。如果你打印两个相同值的表示

两次,你不能确定你得到两次相同的字符串。


If you print the same value to printf twice, you can be sure you get the
same string twice. If you print the representation of two identical values
twice, you can''t be sure you get the same string twice.



你不能确定你会从printf得到两个相同的

字符串("%p \ n%p \ n,p,p)。 printf()是

更好只有在scanf()才能读回字符串后的
; 质量输出可能是

更差或更好在任何一种情况下。


无论如何,它是关于函数指针,为此

printf()不起作用,所以...


Yevgen

You can not be sure that you''ll get two identical
strings from printf("%p\n%p\n", p, p). printf() is
"better" only in that scanf() will be able to read
the string back; the "quality" of output may be
"worse" or "better" in either case.

Anyway, it was about function pointers, for which
printf() doesn''t work, so...

Yevgen


这篇关于printf()和void *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆