类型指针 [英] Typecasting pointers

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

问题描述

大家好,


在C中是否有效转发指针?


例如。代码片段...考虑int为16位且长为32位。


int *变量,值;


*((长*)变量)++ = value;

*((long *)变量)++ = value;

*((long *)变量)++ = value ;

*((长*)变量)++ =价值;


谢谢,

Nishu

Hi All,

Is it valid in C to typecast a pointer?

eg. code snippet... considering int as 16 bit and long as 32 bit.

int *variable, value;

*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;

Thanks,
Nishu

推荐答案

Nishu写道:
Nishu wrote:

大家好,


在C中是否有效转换指针?
Hi All,

Is it valid in C to typecast a pointer?



如果符合对齐要求,则为是。但是,除了一些

例外,您可以将结果转换为原始类型。

If alignment requirements are met, then yes. However, with a few
exceptions, all you are allowed to do with the result is convert it
back to the original type.


例如。代码片段...考虑int为16位且长为32位。


int *变量,值;


*((长*)变量)++ = value;

*((long *)变量)++ = value;

*((long *)变量)++ = value ;

*((long *)变量)++ = value;
eg. code snippet... considering int as 16 bit and long as 32 bit.

int *variable, value;

*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;



这是不允许的。即使你能够将''变量''转换为

指针到长指针,你也可以不使用这个指针来访问

不是什么真的很长。

This is not allowed. Even when you are able to convert ''variable'' to a
pointer-to-long, you may not use this pointer to access anything that
isn''t really a long.





1月29日下午2:59,Harald van D? 3K" < true ... @ gmail.comwrote:


On Jan 29, 2:59 pm, "Harald van D?3k" <true...@gmail.comwrote:

Nishu写道:
Nishu wrote:

大家好,
Hi All,


在C中是否有效转换指针?如果满足对齐要求,则为是。但是,只需要几个
Is it valid in C to typecast a pointer?If alignment requirements are met, then yes. However, with a few



例外,你可以将结果转换为原始类型。 b
。 br />

exceptions, all you are allowed to do with the result is convert it
back to the original type.


例如。代码片段...将int视为16位,长为32位。
eg. code snippet... considering int as 16 bit and long as 32 bit.


int * variable,value;
int *variable, value;


*((long *)变量)++ = value;

*((long *)变量)+ + =值;

*((long *)变量)++ = value;

*((long *)变量)++ = value;这是不允许的。即使你能够将''变量''转换为
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;This is not allowed. Even when you are able to convert ''variable'' to a



指针到长指针,你也可以不使用这个指针访问任何东西

真的不长。

pointer-to-long, you may not use this pointer to access anything that
isn''t really a long.



如果价值很长......

长值;


我''在我的编译器上收到警告..已投射的对象是

而不是l值。


谢谢,

Nishu

In case value is long...
long value;

I''m getting warnings on my compiler.."objects that have been cast are
not l-value."

Thanks,
Nishu


" Nishu" < na ********** @ gmail.comwrites:
"Nishu" <na**********@gmail.comwrites:

1月29日下午2:59,Harald van D?3k ; < true ... @ gmail.comwrote:
On Jan 29, 2:59 pm, "Harald van D?3k" <true...@gmail.comwrote:

> Nishu写道:
>Nishu wrote:

大家好,
Hi All,


在C中是否有效转换指针?如果满足对齐要求,则为是。但是,如果只有几个
Is it valid in C to typecast a pointer?If alignment requirements are met, then yes. However, with a few


例外,你可以将结果转换回原来的类型。

exceptions, all you are allowed to do with the result is convert it
back to the original type.


例如。代码片段...将int视为16位,长为32位。
eg. code snippet... considering int as 16 bit and long as 32 bit.


int * variable,value;
int *variable, value;


*((long *)变量)++ = value;

*((long *)变量)+ + =值;

*((long *)变量)++ = value;

*((long *)变量)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;


这是不允许的。即使你能够将''变量''转换为指向长指针,你也可以不使用这个指针来访问
实际上并不长的任何东西。

This is not allowed. Even when you are able to convert ''variable'' to a
pointer-to-long, you may not use this pointer to access anything that
isn''t really a long.



如果价值很长......

长值;


我''在我的编译器上收到警告..已经投射的对象是

而不是l-value。


In case value is long...
long value;

I''m getting warnings on my compiler.."objects that have been cast are
not l-value."



通过将其替换为int *

来取出由强制转换引起的UB,您应该得到相同的消息。它准确地描述了和b / b
简洁地说错了什么。演员表的结果不是左值 - 它

永远不会表示可以改变的对象。在你的情况下,


*((int *)变量)++


会尝试增加某些东西,但是演员的结果无论在

演员中使用何种类型,都不会增加

。你应该从更明显的信息中得到消息:


int x;

((int)x)++; / *错误...演员阵容不会左右* /


-

Ben。

Take out the UB caused by the cast to long * by replacing it with int *
and you should get the same message. It describes exactly and
succinctly what is wrong. The result of a cast is not an lvalue -- it
never denotes an object that can be changed. In your case,

*((int *)variable)++

would try to increment something, but the result of a cast is never a
thing that can be incremented, no matter what the type is used in the
cast. You should get the message from the more obvious:

int x;
((int)x)++; /* error... casts do not make lvalues */

--
Ben.


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

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