char *可以指向任何字节吗?! [英] char * can point to any byte?!

查看:98
本文介绍了char *可以指向任何字节吗?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下网站上常见问题

http: //c-faq.com/strangeprob/ptralign.html


"通过将char *(可以指向任何字节)转换为int *或

long int *,然后指向它,你最终可能会要求

处理器从未对齐的地址获取多字节值,这是
它不愿意这样做。


char *如何指向任何字节,但int *不能?有人可以澄清这个吗?


谢谢

乍得

解决方案

Chadaécrit:

char *如何指向任何字节,但int *不能?有人可以澄清这个吗?




char *是一个指向char的指针,它是一个字节的大小。

一个int可以由多个字节定义。




loufoque写道:

Chadaécrit:

char *如何指向任何字节,但int *不能?有人可以澄清这个吗?



char *是一个指向char的指针,它是一个字节的大小。
一个int可以由多个字节定义。




换句话说,char *可以指向int中的第2或第3个字节?

"乍得" < CD ***** @ gmail.com>写道:

在以下网站上常见问题

http://c-faq.com/strangeprob/ptralign.html

"通过将char *(可以指向任何字节)转换为int *或
long int *,然后指向它,你最终可能要求
处理器从未对齐的地址获取多字节值,这是不愿意做的。

char *如何指向任何字节,但int *不能?有人可以澄清这个吗?




例如,在某些系统上,int是4个字节长,并且只能是

存储在4的倍数的地址。在这种情况下,int *

不能合法地指向奇数地址。


细节将因系统而异。在某些系统上,

int *具有与char *不同的表示形式,并且int *不是
甚至物理上能够指向单个字节。


-

Keith Thompson(The_Other_Keith) ks *** @ mib .org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


On the following sites faq

http://c-faq.com/strangeprob/ptralign.html

"By converting a char * (which can point to any byte) to an int * or
long int *, and then indirecting on it, you can end up asking the
processor to fetch a multibyte value from an unaligned address, which
it isn''t willing to do. "

How can char * point to any byte, but int * can''t? Can someone clarify
this?

Thanks
Chad

解决方案

Chad a écrit :

How can char * point to any byte, but int * can''t? Can someone clarify
this?



char* is a pointer to a char, which is the size of a byte.
an int may be defined by multiple bytes.



loufoque wrote:

Chad a écrit :

How can char * point to any byte, but int * can''t? Can someone clarify
this?



char* is a pointer to a char, which is the size of a byte.
an int may be defined by multiple bytes.



So in other words, char * could point to the 2nd or 3rd byte in int?


"Chad" <cd*****@gmail.com> writes:

On the following sites faq

http://c-faq.com/strangeprob/ptralign.html

"By converting a char * (which can point to any byte) to an int * or
long int *, and then indirecting on it, you can end up asking the
processor to fetch a multibyte value from an unaligned address, which
it isn''t willing to do. "

How can char * point to any byte, but int * can''t? Can someone clarify
this?



For example, on some systems an int is 4 bytes long, and can only be
stored at an address that''s a multiple of 4. In that case, an int*
cannot legally point to an odd address.

The details will vary from one system to another. On some systems, an
int* has a different representation than a char*, and an int* isn''t
even physically capable of pointing to a single byte.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于char *可以指向任何字节吗?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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