char *,unsigned char *和POD类型 [英] char *, unsigned char * and POD types
问题描述
起初代码似乎无法正常工作。任何想法?:
#include< iostream>
#include< cstdlib>
int main()
{
使用命名空间std;
int x = 7;
char * p = reinterpret_cast< char *>(& x);
for(size_t i = 0; i< sizeof(x); ++ i)
cout<< P [1] - ;< ''\ n'';
}
还有两个问题。
Q1 )上面的char *使用是否保证适用于所有POD类型?
Q2)如果我记得很清楚,unsigned char *涵盖更多类型。我错了,
它只包括POD类型?
提前谢谢。
< blockquote> john写道:
起初代码似乎无法正常工作。
定义似乎无法正常工作。
有什么想法吗?:
#include< iostream>
#include< cstdlib>
int main()
{
使用命名空间std;
int x = 7;
char * p = reinterpret_cast< char *>(& x);
for(size_t i = 0; i< sizeof(x); ++ i)
COUT<< P [1] - ;< \\\
;
这会将字节打印为字符。
>
}
还有两个问题。
Q1)上面的char *使用是否适用于所有POD类型?
IIRC,reinterpret_cast的结果未指定。
Q2)如果我记得很清楚,无符号char *涵盖更多类型。我错了,
它只包括POD类型?
对于char和unsigned char,它是相同的。仅涵盖了POD类型。
Rolf Magnus写道:
john写道:
>起初代码似乎不起作用。
定义似乎无法正常工作。
>任何想法?:
#include< iostream>
#include< cstdlib>
int main()
{
使用命名空间std;
int x = 7;
char * p = reinterpret_cast< char *>(& x);
for(size_t i = 0 ; i< sizeof(x); ++ i)
cout<< P [1] - ;< \\\
;
这会将字节打印为字符。
对,你(OP)期望输出什么?在大多数计算机上,一个
字符为0x07,另一个为3 0x00或空值。如果你想看到
你需要将字符值转换为
a数字的字节值(数值)。
cout<< static_cast< int>(p [i])<< ''\\ n';;
可能会给你你所期望的,虽然你没有说出你的期望所以
我只能猜测。您可能想要使用static_cast< unsigned int> ;.
>}
还有两个问题。
Q1)以上char *使用是否保证适用于所有POD类型?
IIRC,reinterpret_cast的结果未指定。
AFAIK没有设定要求C或C ++程序以任何特定方式存储他们的
数字,只要他们遵循要求,IE
sizeof char< = sizeof int< = sizeof long int等...计算机可以使用
它认为最好的内部存储器。所以该程序的输出是未指定的
,对于bigendian和小端机器上的事实,你将获得不同的输出。
> Q2)如果我记得很清楚,unsigned char *涵盖更多类型。我错了,它只涵盖了POD类型吗?
对于char和unsigned char,它是相同的。仅涵盖POD类型。
我不明白这个问题。 覆盖更多类型什么?基本上
你想要做的事情(或者我认为)很接近,你只需要将一个字符转换成一个数字来看看它的价值。 br />
-
Jim Langston
ta * ******@rocketmail.com
john< jo ** @ no.spamwrote in comp.lang.c ++:
#include< iostream>
#include< cstdlib>
int main()
{
使用命名空间std;
int x = 7;
char * p = reinterpret_cast< char *>(& x);
for(size_t i = 0; i< sizeof(x); ++ i)
cout<< P [1] - ;< ''\ n'';
}
还有两个问题。
Q1 )上面的char *使用是否保证适用于所有POD类型?
Q2)如果我记得很清楚,unsigned char *涵盖更多类型。我错了,
它只包括POD类型?
A" plain" char应该只用于存储字符。如果你想要使用一个字节用于不同目的(例如存储数字),那么
将使用unsigned char或signed char。
如果你试图打印一个对象的字节,那么下面的
代码是完全定义和可移植的:
#include< iostream>
模板< class T>
void PrintBytes(T const& obj)
{< br $>
char unsigned const volatile * p =
reinterpret_cast< char unsigned const volatile *>(& obj);
char unsigned const volatile * const pend = p + sizeof obj;
do std :: cout<< * p ++;
while(pend!= p);
}
尽管有人暗示相反,行为
reinterpret_cast非常明确。
-
$ b $bTomásóhéilidhe
Hi, at first the code doesn''t seem to work. Any ideas?:
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
int x= 7;
char *p= reinterpret_cast<char *>(&x);
for(size_t i= 0; i< sizeof(x); ++i)
cout<< p[i]<< ''\n'';
}
Two more questions.
Q1) Is the above char * use guaranteed to work with all POD types?
Q2) If I remember well, unsigned char * covers more types. Am I wrong,
and it covers only POD types?
Thanks in advance.
john wrote:
Hi, at first the code doesn''t seem to work.Define "doesn''t seem to work".
Any ideas?:
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
int x= 7;
char *p= reinterpret_cast<char *>(&x);
for(size_t i= 0; i< sizeof(x); ++i)
cout<< p[i]<< ''\n'';This will print the bytes as characters.
>
}
Two more questions.
Q1) Is the above char * use guaranteed to work with all POD types?IIRC, the result of the reinterpret_cast is unspecified.
Q2) If I remember well, unsigned char * covers more types. Am I wrong,
and it covers only POD types?It''s the same for char and unsigned char. Only POD types are covered.
Rolf Magnus wrote:john wrote:
>Hi, at first the code doesn''t seem to work.
Define "doesn''t seem to work".
>Any ideas?:
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
int x= 7;
char *p= reinterpret_cast<char *>(&x);
for(size_t i= 0; i< sizeof(x); ++i)
cout<< p[i]<< ''\n'';
This will print the bytes as characters.Right, what did you (the OP) expect it to output? On most computers, one
character would be 0x07 and the other 3 0x00 or nulls. If you wanted to see
the value of the bytes (the numerical value) you''ll need to cast the char to
a number.
cout<< static_cast<int>( p[i] ) << ''\n'';
may give you what you expect, although you haven''t stated what you expect so
I can only guess. You may want to use static_cast<unsigned int>.
>}
Two more questions.
Q1) Is the above char * use guaranteed to work with all POD types?
IIRC, the result of the reinterpret_cast is unspecified.AFAIK there is no set requirement for a C or C++ program to store their
numbers in any particular way as long as they follow the requirements, I.E.
sizeof char <= sizeof int <= sizeof long int etc... A computer could use
whatever internal storage it deems best. So the output of the program is
unspecified, and for a fact on bigendian and little endian machines you will
get different outputs.
>Q2) If I remember well, unsigned char * covers more types. Am I
wrong, and it covers only POD types?
It''s the same for char and unsigned char. Only POD types are covered.I don''t understand the question. "covers more types" of what? Basically
what you are trying to do (or so I think) is close, you just need to cast
the character to a number to see the value of it.
--
Jim Langston
ta*******@rocketmail.com
john <jo**@no.spamwrote in comp.lang.c++:
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
int x= 7;
char *p= reinterpret_cast<char *>(&x);
for(size_t i= 0; i< sizeof(x); ++i)
cout<< p[i]<< ''\n'';
}
Two more questions.
Q1) Is the above char * use guaranteed to work with all POD types?
Q2) If I remember well, unsigned char * covers more types. Am I wrong,
and it covers only POD types?
A "plain" char should only be used for storing characters. If you
want to use a byte for a different purpose (e.g. storing numbers), then
go with unsigned char or signed char.
If you''re trying to print the bytes of an object, then the following
code is perfectly well-defined and portable:
#include <iostream>
template<class T>
void PrintBytes(T const &obj)
{
char unsigned const volatile *p =
reinterpret_cast<char unsigned const volatile*>(&obj);
char unsigned const volatile *const pend = p + sizeof obj;
do std::cout << *p++;
while (pend != p);
}
Despite someone has suggested to the contrary, the behaviour of the
reinterpret_cast is perfectly well-defined.
--
Tomás ó héilidhe
这篇关于char *,unsigned char *和POD类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!