指针转换 [英] pointer conversion

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

问题描述

伙计们,


我有一个关于指针转换的问题。请查看

以下代码片段。


char * cptr;

int * iptr;


/ *这里的一些代码初始化iptr * /


cptr =(char *)iptr; / *第1行

* /

cptr = cptr + sizeof(int); / *第2行* /

iptr =(int *)cptr; / *第3行* /


现在根据指针转换规则:

Page 47:n1124.pdf


指向对象或不完整类型的指针可能会转换为指向不同对象或不完整类型的指针。如果得到的

指针没有正确对齐57)对于指向类型,则

行为未定义。否则,当再次转换回来时,

结果将等于原始指针。


根据上述转换规则,第1行应该是完全没问题的。

但是,我想知道转换是否在第3行完成。允许或

没有。

我有这个疑问因为cptr已被更改。

但是,它正确对齐以指向整数对象。

guys,

I have a question regarding pointer conversion. Please look at the
following code snippet.

char *cptr;
int *iptr;

/* Some code here that initializes "iptr" */

cptr = (char *)iptr; /* Line 1
*/
cptr = cptr + sizeof(int); /* Line 2 */
iptr = (int *)cptr; /* Line 3 */

Now as per the pointer conversion rule:
Page 47: n1124.pdf

A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned57) for thepointed-to type, the
behavior is undefined. Otherwise, when converted back again, the
result shall compare equal to the original pointer.

According to above conversion rule, "Line 1" should be perfectly fine.
But, I want to know if the conversion done at "Line 3" is allowed or
not.
I have this doubt because the "cptr" has been changed.
But, it is properly aligned to point to an integer object.

推荐答案



< ju ********** @ yahoo.co.inwrote in message

news:11 ********** ************@i38g2000prf.googlegr oups.com ...

<ju**********@yahoo.co.inwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...

guys,


我有关于指针转换的问题。请查看

以下代码片段。


char * cptr;

int * iptr;


/ *这里的一些代码初始化iptr * /


cptr =(char *)iptr; / *第1行

* /

cptr = cptr + sizeof(int); / *第2行* /

iptr =(int *)cptr; / *第3行* /


现在根据指针转换规则:

Page 47:n1124.pdf


指向对象或不完整类型的指针可能会转换为指向不同对象或不完整类型的指针。如果得到的

指针没有正确对齐57)对于指向类型,则

行为未定义。否则,当再次转换回来时,

结果将等于原始指针。


根据上述转换规则,第1行应该是完全没问题的。

但是,我想知道转换是否在第3行完成。允许或

没有。

我有这个疑问因为cptr已被更改。

但是,它正确对齐以指向整数对象。
guys,

I have a question regarding pointer conversion. Please look at the
following code snippet.

char *cptr;
int *iptr;

/* Some code here that initializes "iptr" */

cptr = (char *)iptr; /* Line 1
*/
cptr = cptr + sizeof(int); /* Line 2 */
iptr = (int *)cptr; /* Line 3 */

Now as per the pointer conversion rule:
Page 47: n1124.pdf

A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned57) for thepointed-to type, the
behavior is undefined. Otherwise, when converted back again, the
result shall compare equal to the original pointer.

According to above conversion rule, "Line 1" should be perfectly fine.
But, I want to know if the conversion done at "Line 3" is allowed or
not.
I have this doubt because the "cptr" has been changed.
But, it is properly aligned to point to an integer object.



你会没事的。

char总是1个字节。因此,将一个任意指针转换为char *,添加

与原始类型大小完全相同的数量,然后返回

保证保持对齐。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

You will be OK.
char is always 1 byte. So casting an arbitrary pointer to a char *, adding
an exact multiple of the size of the original type, and casting back is
guaranteed to preserve alignment.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


>>>>" M" == Malcolm McLean< re ******* @ btinternet.comwrites:


你会好的。 char总是1个字节。因此,将一个任意的

Mpointer投射到一个char *,添加一个原始类型大小为

的精确倍数,并且保证退回保证

赔偿。


我对此不太确定;你是否愿意引用C& V,如果

你声称它是由标准保证的吗?


Charlton

-

Charlton Wilbur
cw ***** @ chromatico.net
>>>>"M" == Malcolm McLean <re*******@btinternet.comwrites:

MYou will be OK. char is always 1 byte. So casting an arbitrary
Mpointer to a char *, adding an exact multiple of the size of
Mthe original type, and casting back is guaranteed to preserve
Malignment.

I am not so sure about that; would you care to cite C&V, please, if
you claim that it''s guaranteed by the standard?

Charlton
--
Charlton Wilbur
cw*****@chromatico.net




" Charlton Wilbur" < cw ***** @ chromatico.netwrote in message

news:87 ************ @ mithril.chromatico.net ...

"Charlton Wilbur" <cw*****@chromatico.netwrote in message
news:87************@mithril.chromatico.net...

>>>>>"""""""""""""""""""" == Malcolm McLean< re ******* @ btinternet.comwrites:
>>>>>"M" == Malcolm McLean <re*******@btinternet.comwrites:



MYou会好的。 char总是1个字节。因此,将一个任意的

Mpointer投射到一个char *,添加一个原始类型大小为

的精确倍数,并且保证退回保证

赔偿。


我对此不太确定;你是否愿意引用C& V,如果

你声称它是由标准保证的?


MYou will be OK. char is always 1 byte. So casting an arbitrary
Mpointer to a char *, adding an exact multiple of the size of
Mthe original type, and casting back is guaranteed to preserve
Malignment.

I am not so sure about that; would you care to cite C&V, please, if
you claim that it''s guaranteed by the standard?



阵列有在记忆中连续。没有填充字节可以插入

项目之间。

其余部分来自于此。


-

免费游戏和编程好东西。
http:// www.personal.leeds.ac.uk/~bgy1mm

Arrays have to be contiguous in memory. No padding bytes may be inserted
between items.
The rest follows from that.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


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

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