I want to get a detailed explanation on the difference between using %d
and %p
type for printing pointer
.
我想要得到一个关于使用%d和%p类型打印指针的区别的详细说明。
Also Why does %p
return hexadecimal? What are the cases when %d
and %p
return different values? Does datatype only represent the way the user wants the output or it has something to do with the memory locations too?
为什么%p返回十六进制?当%d和%p返回不同的值时,情况是什么?数据类型是否只表示用户想要输出的方式,还是与内存位置有关?
3 个解决方案
#1
11
For the program to be well-defined, the format specifier must match the type of the argument. Therefore you can use %p
but not %d
to print out pointers. (The latter might happen to work on some architectures but is technically undefined behaviour.)
对于要定义的程序,格式说明符必须与参数的类型相匹配。因此,您可以使用%p而不是%d打印指针。(后者可能发生在某些架构上,但在技术上是未定义的行为。)
The primary reason you can't freely interchange %d
and %p
is that ints
and pointers don't have to have the same size.
不能*交换%d和%p的主要原因是int和指针不需要具有相同的大小。
The format in which pointers are printed out is architecture-specific (pointers can have different size or indeed different structure). It is, however, common to transcribe memory addresses in hexadecimal, so this is what %p
usually does.
指针打印出来的格式是特定于体系结构的(指针可以有不同的大小或不同的结构)。然而,在十六进制中抄写内存地址是很常见的,所以这就是%p通常所做的。
#2
1
Those conversions are highly architecture dependent. One of the most clear distinctions are with the real mode 8086 where int
is 16 bits and a (large model) pointer is 32 bits but has a segment and offset which are always written as segment:offset.
这些转换高度依赖于体系结构。最明显的区别之一是真正的模式8086,其中int是16位,一个(大模型)指针是32位,但是有一个段和偏移量,它们总是被写为段:偏移。
%d takes 16 bits and displays it as a signed value 123
%p takes a pointer and display it in address format 0fef:0004
Since %p
was introduced relatively recently I don't know of any implementations but a PDP-11 library ought to implement it by display the 16-bit address in octal.
由于%p是在最近引入的,我不知道有什么实现,但是PDP-11库应该通过在八进制中显示16位地址来实现它。
#3
0
A pointer of variable in C has value just like other type of variable such as the int、char and so on. The %p format string in the printf function just indicates that the type of parameter "i" is pointer to the printf instead of int. Thus printf outputs the Hex value of parameter i because printf seem the parameter i as a pointer type. No matter what the type of variable i is, the value is same----10 or 0xa. The difference between the types of i---int or pointer ----is the different ways to use in C. if type of i is regarded as pointer, you can visit the memory specified by value of pointer i or other operations the pointer type supports. If type of i is int, we can do some operations such as addition or subtraction rather than visiting the memory by using value of i, because the grammar of C do't allow you to do that(just warning). if you know what you want to do, you can do that.
C中的变量指针与其他类型的变量(例如int、char等)具有相同的值。printf函数中的%p格式字符串表示参数“i”的类型是指向printf的指针,而不是int类型,因此printf输出参数i的十六进制值,因为printf似乎是作为指针类型的参数i。不管变量i的类型是什么,值都是一样的——10或0xa。i -int或指针类型之间的区别是在c中使用的不同方法。如果将i类型视为指针,则可以访问由指针类型支持的指针i或其他操作指定的内存。如果我是int类型的,我们可以做一些操作,例如添加或减法,而不是使用i的值访问内存,因为C的语法不允许您这样做(只是警告)。如果你知道你想做什么,你就能做到。
#1
11
For the program to be well-defined, the format specifier must match the type of the argument. Therefore you can use %p
but not %d
to print out pointers. (The latter might happen to work on some architectures but is technically undefined behaviour.)
对于要定义的程序,格式说明符必须与参数的类型相匹配。因此,您可以使用%p而不是%d打印指针。(后者可能发生在某些架构上,但在技术上是未定义的行为。)
The primary reason you can't freely interchange %d
and %p
is that ints
and pointers don't have to have the same size.
不能*交换%d和%p的主要原因是int和指针不需要具有相同的大小。
The format in which pointers are printed out is architecture-specific (pointers can have different size or indeed different structure). It is, however, common to transcribe memory addresses in hexadecimal, so this is what %p
usually does.
指针打印出来的格式是特定于体系结构的(指针可以有不同的大小或不同的结构)。然而,在十六进制中抄写内存地址是很常见的,所以这就是%p通常所做的。
#2
1
Those conversions are highly architecture dependent. One of the most clear distinctions are with the real mode 8086 where int
is 16 bits and a (large model) pointer is 32 bits but has a segment and offset which are always written as segment:offset.
这些转换高度依赖于体系结构。最明显的区别之一是真正的模式8086,其中int是16位,一个(大模型)指针是32位,但是有一个段和偏移量,它们总是被写为段:偏移。
%d takes 16 bits and displays it as a signed value 123
%p takes a pointer and display it in address format 0fef:0004
Since %p
was introduced relatively recently I don't know of any implementations but a PDP-11 library ought to implement it by display the 16-bit address in octal.
由于%p是在最近引入的,我不知道有什么实现,但是PDP-11库应该通过在八进制中显示16位地址来实现它。
#3
0
A pointer of variable in C has value just like other type of variable such as the int、char and so on. The %p format string in the printf function just indicates that the type of parameter "i" is pointer to the printf instead of int. Thus printf outputs the Hex value of parameter i because printf seem the parameter i as a pointer type. No matter what the type of variable i is, the value is same----10 or 0xa. The difference between the types of i---int or pointer ----is the different ways to use in C. if type of i is regarded as pointer, you can visit the memory specified by value of pointer i or other operations the pointer type supports. If type of i is int, we can do some operations such as addition or subtraction rather than visiting the memory by using value of i, because the grammar of C do't allow you to do that(just warning). if you know what you want to do, you can do that.
C中的变量指针与其他类型的变量(例如int、char等)具有相同的值。printf函数中的%p格式字符串表示参数“i”的类型是指向printf的指针,而不是int类型,因此printf输出参数i的十六进制值,因为printf似乎是作为指针类型的参数i。不管变量i的类型是什么,值都是一样的——10或0xa。i -int或指针类型之间的区别是在c中使用的不同方法。如果将i类型视为指针,则可以访问由指针类型支持的指针i或其他操作指定的内存。如果我是int类型的,我们可以做一些操作,例如添加或减法,而不是使用i的值访问内存,因为C的语法不允许您这样做(只是警告)。如果你知道你想做什么,你就能做到。