什么是正确使用的printf显示指针用0填充 [英] What's the proper use of printf to display pointers padded with 0s

查看:998
本文介绍了什么是正确使用的printf显示指针用0填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,我想使用printf显示指针,使他们正确地排队,我想用0垫他们。

In C, I'd like to use printf to display pointers, and so that they line up properly, I'd like to pad them with 0s.

我的猜测是,这样做的正确方法是:

My guess was that the proper way to do this was:

printf("%016p", ptr);

这工作,但这个GCC与以下消息抱怨:

This works, but this gcc complains with the following message:

warning: '0' flag used with ‘%p’ gnu_printf format

我GOOGLE了一下吧,下面线程是关于同一主题,但并没有真正给出一个解决方案。

I've googled a bit for it, and the following thread is on the same topic, but doesn't really give a solution.

<一个href=\"http://gcc.gnu.org/ml/gcc-bugs/2003-05/msg00484.html\">http://gcc.gnu.org/ml/gcc-bugs/2003-05/msg00484.html

阅读它,看来为什么GCC抱怨的原因是我建议的语法不在C99定义。但我似乎无法找到任何其他方式做同样的事情在一个标准的批准方式。

Reading it, it seems that the reason why gcc complains is that the syntax I suggested is not defined in C99. But I can't seem to find any other way to do the same thing in a standard approved way.

因此​​,这里的双待:

So here is the double question:


  • 我的理解是正确的,这种行为是不是由C99标准界定?
  • 如果是这样,有一个标准的批准,这样做的可移植的方法?
  • Is my understanding correct that this behavior is not defined by the C99 standard?
  • If so, is there a standard approved, portable way of doing this?

推荐答案

的#include&LT; inttypes.h&GT;

的#include&LT; stdint.h&GT;

的printf(%016PRIxPTR\\ n,(uintptr_t形式)PTR);

但它不会打印在执行定义的方式指针(称 DEAD:牛肉 8086分段模式)。

but it won't print the pointer in the implementation defined way (says DEAD:BEEF for 8086 segmented mode).

这篇关于什么是正确使用的printf显示指针用0填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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