%p 的 printf 格式说明符标志为“0”,正确与否?
printf format specifier flags '0' for %p, correct or not?
将“0”作为标志附加到 printf
%p 格式说明符是否正确?
#include <stdio.h>
int main()
{
int a = 42;
void* p = &a;
printf("Pointer address: %08p", p);
return 0;
}
也在 Ideone.
编译一些与上述类似的代码,我没有收到来自 Microsoft Visual C++ 2015 的警告或任何警告,但收到来自 GCC 5.4.0 的警告:
"warning: '0' flag used with '%p' gnu_printf format [-Wformat]"
阅读 cppreference printf,我看到:
0 : for integer and floating point number conversions, leading zeros
are used to pad the field instead of space characters. For integer
numbers it is ignored if the precision is explicitly specified. For
other conversions using this flag results in undefined behavior. It is
ignored if - flag is present.
据我所知,%p 是指针地址,毕竟它是一个整数,所以这个未定义的行为适用于 %p 吗?
如果不是,为什么 GCC -Wformat 警告?
%p is for pointer address, which is an integer number after all,
不,从 C/C++ 类型系统的角度来看,指针不是整数。
指针既不是算术类型也不是整数类型,迂腐见std::is_integral, std::is_arithmetic。
%p
is for pointer address, which is an integer number after all
尽管指针确实具有数字表示形式,但标准并未将指针视为整型数据类型。因此,使用 %08p
格式会导致未定义的行为。
您可以通过 using uintptr_t
data type 解决此问题,将您的指针转换为它,然后将其打印为无符号整数:
#include <cinttypes>
#include <cstdint>
int main() {
int i = 123;
void* p = &i;
printf("%08" PRIxPTR "\n", (uintptr_t)p);
return 0;
}
将“0”作为标志附加到 printf
%p 格式说明符是否正确?
#include <stdio.h>
int main()
{
int a = 42;
void* p = &a;
printf("Pointer address: %08p", p);
return 0;
}
也在 Ideone.
编译一些与上述类似的代码,我没有收到来自 Microsoft Visual C++ 2015 的警告或任何警告,但收到来自 GCC 5.4.0 的警告:
"warning: '0' flag used with '%p' gnu_printf format [-Wformat]"
阅读 cppreference printf,我看到:
0 : for integer and floating point number conversions, leading zeros are used to pad the field instead of space characters. For integer numbers it is ignored if the precision is explicitly specified. For other conversions using this flag results in undefined behavior. It is ignored if - flag is present.
据我所知,%p 是指针地址,毕竟它是一个整数,所以这个未定义的行为适用于 %p 吗?
如果不是,为什么 GCC -Wformat 警告?
%p is for pointer address, which is an integer number after all,
不,从 C/C++ 类型系统的角度来看,指针不是整数。
指针既不是算术类型也不是整数类型,迂腐见std::is_integral, std::is_arithmetic。
%p
is for pointer address, which is an integer number after all
尽管指针确实具有数字表示形式,但标准并未将指针视为整型数据类型。因此,使用 %08p
格式会导致未定义的行为。
您可以通过 using uintptr_t
data type 解决此问题,将您的指针转换为它,然后将其打印为无符号整数:
#include <cinttypes>
#include <cstdint>
int main() {
int i = 123;
void* p = &i;
printf("%08" PRIxPTR "\n", (uintptr_t)p);
return 0;
}