输出一个指向流的NULL指针 [英] outputting a NULL pointer to a stream

查看:91
本文介绍了输出一个指向流的NULL指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您好!


我有一段代码需要使用流显示格式化的

指针表,指针代表

为十六进制值。看起来或多或少是这样的:


#include< iostream>

#include< iomanip>


using namespace std;


// ptr []是一个指针数组


int main(){

for(unsigned int i = 0; i< some_size; ++ i){


cout<< hex<< setprecision(16)<< setw(20)

<< (void *)ptr [i]<< ''\\ n'';


}


}


让我烦恼的是如果ptr [i]为NULL,那么确实

输出为''0'而不是

''0x0000000000000000''因为我喜欢它打印。


除了

手动检查NULL之外,有人可以建议一个解决方案吗?转换为int不是一个

选项,因为指针是8字节,整数是4字节

在我的系统上。


提前谢谢,

- J.

解决方案

Jacek Dziedzic写道:

你好我有一段代码需要使用流显示格式化的指针表,指针表示为十六进制值。它看起来或多或少是这样的:

< iostream>
#include< iomanip>

使用命名空间std;

// ptr []是一个指针数组

int main(){
for(unsigned int i = 0; i< some_size; ++ i){

cout<< hex<< setprecision(16)<< setw(20)
<< (void *)ptr [i]<< ''\\ n'';

}



让我恼火的是,如果ptr [i]为NULL,那么它确实是
输出为''0'而不是
''0x0000000000000000''因为我想要它打印。

任何人都可以提出解决方案,其他比手动检查NULL?转换为int不是一个
选项,因为指针是8字节,而且我的系统上的整数是4字节。

提前感谢,
- J.




使用std :: cout :: fill(''0'')或等效的I / O操纵器。我会打赌

指针,比方说,地址0x100也打印没有零(即''

100'')。顺便说一句,你省略了0x。在你的cout

声明。


M


mlimber写道:


使用std :: cout :: fill(''0'')或等效的I / O操纵器。我敢打赌,指向地址0x100的指针也会在没有零的情况下打印(即''
100'')。顺便说一句,你省略了0x。在你的cout
声明中。




你已经输掉了赌注:)。因为我使用了std :: hex,所以

指针打印出来(例如)


0x2000000002affc00

0x2000000007ccda34

0x2000000009418afb

0

0x2000000009418afb

0x2000000009418afb





而无需手动指定0x并将fill-char

设置为0。但不是NULL,唉。


- J.


" Jacek Dziedzic" < jacek@no_spam.tygrys.no_spam.net>在消息中写道

news:80 ************************** @ news.chello.pl ..。< blockquote class =post_quotes> mlimber写道:


使用std :: cout :: fill(''0'')或等效的I / O操纵器。我敢打赌,指向地址0x100的指针也会在没有零的情况下打印(即''
100'')。顺便说一句,你省略了0x。在你的cout
声明中。



你已经失去了赌注:)。因为我已经使用了std :: hex,所以
指针打印出来(例如)

0x2000000002affc00
0x2000000007ccda34
0x2000000009418afb
0
0x2000000009418afb
0x2000000009418afb


无需手动指定0x并将fill-char
设置为0。但不是NULL,唉。




你错过了什么。你没注意到第一个值是2吗? 0x2 ....

这就是为什么你没有错过任何领先的零,你没有任何东西!


所以他没有但是还没有打赌。



Hello!

I have a piece of code that needs to display a formatted
table of pointers using streams, with the pointers represented
as hex values. It looks more or less like this:

#include <iostream>
#include <iomanip>

using namespace std;

// ptr[] is an array of pointers

int main() {
for(unsigned int i=0;i<some_size;++i) {

cout << hex << setprecision(16) << setw(20)
<< (void*)ptr[i] << ''\n'';

}

}

What annoys me is that if ptr[i] is NULL, then it does
get output as '' 0'' instead of
''0x0000000000000000'' as I''d like it to print.

Can anyone suggest a solution to this, other than
checking for NULL manually? Casting to int is not an
option since pointers are 8-byte and ints are 4-byte
on my system.

thanks in advance,
- J.

解决方案

Jacek Dziedzic wrote:

Hello!

I have a piece of code that needs to display a formatted
table of pointers using streams, with the pointers represented
as hex values. It looks more or less like this:

#include <iostream>
#include <iomanip>

using namespace std;

// ptr[] is an array of pointers

int main() {
for(unsigned int i=0;i<some_size;++i) {

cout << hex << setprecision(16) << setw(20)
<< (void*)ptr[i] << ''\n'';

}

}

What annoys me is that if ptr[i] is NULL, then it does
get output as '' 0'' instead of
''0x0000000000000000'' as I''d like it to print.

Can anyone suggest a solution to this, other than
checking for NULL manually? Casting to int is not an
option since pointers are 8-byte and ints are 4-byte
on my system.

thanks in advance,
- J.



Use std::cout::fill( ''0'' ) or the equivalent I/O manipulator. I''ll bet
a pointer to, say, address 0x100 also prints without the zeros (i.e., ''
100''). BTW, you omitted the "0x" in your cout
statement.

M


mlimber wrote:


Use std::cout::fill( ''0'' ) or the equivalent I/O manipulator. I''ll bet
a pointer to, say, address 0x100 also prints without the zeros (i.e., ''
100''). BTW, you omitted the "0x" in your cout
statement.



You''ve lost your bet :). Since I''ve used std::hex, the
pointers print out as (for instance)

0x2000000002affc00
0x2000000007ccda34
0x2000000009418afb
0
0x2000000009418afb
0x2000000009418afb

etc.

without having to manually specifying "0x" and setting the fill-char
to "0". But not for NULL, alas.

- J.


"Jacek Dziedzic" <jacek@no_spam.tygrys.no_spam.net> wrote in message
news:80**************************@news.chello.pl.. .

mlimber wrote:


Use std::cout::fill( ''0'' ) or the equivalent I/O manipulator. I''ll bet
a pointer to, say, address 0x100 also prints without the zeros (i.e., ''
100''). BTW, you omitted the "0x" in your cout
statement.



You''ve lost your bet :). Since I''ve used std::hex, the
pointers print out as (for instance)

0x2000000002affc00
0x2000000007ccda34
0x2000000009418afb
0
0x2000000009418afb
0x2000000009418afb

etc.

without having to manually specifying "0x" and setting the fill-char
to "0". But not for NULL, alas.



You missed something. Don''t you notice the first value is 2? 0x2....
that''s why you''re not missing any leading zeros, you dont'' have any!

So he hasn''t lost the bet yet.


这篇关于输出一个指向流的NULL指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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