Querry on pointers [英] Querry on pointers

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

问题描述

大家好,


char a [2]

char * u8

int u32


u8 =& u32


我们不能指出一个指向int的char指针吗?

为什么这不可能?


我想将数组a中的所有数据传输到u32。

谢谢

Hello all,

char a[2]
char* u8
int u32

u8 = &u32

Cant we referance a char pointer to int ?
Why is this not possible?

I want to transfer all the data from array a to u32.
Thanks

推荐答案

abhaybhat写道:
abhaybhat wrote:

大家好,


char a [2]

char * u8

int u32

u8 =& u32
Hello all,

char a[2]
char* u8
int u32

u8 = &u32



你想分配一个int *到char *?这听起来像是混淆的秘诀。

You want to assign an int* to a char*? That sounds
like a recipe for confusion.


我们不能指出一个指向int的char指针吗?

为什么这不可能吗?
Cant we referance a char pointer to int ?
Why is this not possible?



这是/可能/。这可能不是/明智/,取决于。

It''s /possible/. It''s likely not /wise/, depending.


我想将所有数据从数组a传输到u32。
I want to transfer all the data from array a to u32.



u32 =(a [1]<<< 8)+ a [0];


(其中` 8`应该拼写为CHAR_BIT和1

和0可能需要互换,具体取决于你为字节选择的
字节顺序。)


不需要指针式游戏。


-

阴影从哪里出来" /白色房间/


Hewlett-Packard Limited注册号:

注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597英格兰

u32 = (a[1] << 8) + a[0];

(where `8` should likely be spelled `CHAR_BIT` and `1`
and `0` may need interchanging depending on what
endianness you''ve picked for the bytes.)

No pointer-type-games needed.

--
"Where the shadows run from themselves." /White Room/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


abhaybhat写道:
abhaybhat wrote:

Hello all,


char a [ 2]

char * u8

int u32

u8 =& u32


我们不能指出一个指向int的char指针吗?
Hello all,

char a[2]
char* u8
int u32

u8 = &u32

Cant we referance a char pointer to int ?



不可移植。类型为T *的指针可以保存值为NULL或类型为T的对象的

地址。特殊指针void *可以保留NULL

或者地址任何对象类型,但可能不会被引用。你也不能在无效的情况下做指针算法。

Not portably. A pointer of type T* may hold either the value NULL or the
address of an object of type T. The special pointer void* may hold NULL
or the address of any object type, but may not be deferenced. Nor can
you do pointer arithmetic on void*.


为什么这不可能?
Why is this not possible?



这就是语言的设计方式。主要原因是

在系统上的可实现性,其中不同的指针类型可能是彼此不兼容的,即它们的大小可能不同,它们的

表示可能有所不同等。


< snip>

That''s how the language has been designed. The main reason is
implementability on systems where different pointer types may be
incompatible with each other, i.e., their size may be different, their
representation may be different, etc.

<snip>


6月12日,3:38下午,Chris Dollin< chris.dol ... @ hp.comwrote:
On Jun 12, 3:38 pm, Chris Dollin <chris.dol...@hp.comwrote:

abhaybhat写道:
abhaybhat wrote:

大家好,
Hello all,


char a [2]

char * u8

int u32
char a[2]
char* u8
int u32


u8 =& u32
u8 = &u32



你想要一个int *来分配一个char *?这听起来像是混淆的秘诀。


You want to assign an int* to a char*? That sounds
like a recipe for confusion.


我们不能指出一个指向int的char指针吗?

为什么这不可能吗?
Cant we referance a char pointer to int ?
Why is this not possible?



这是/可能/。这可能不是/明智/,取决于。


It''s /possible/. It''s likely not /wise/, depending.


我想将所有数据从数组a传输到u32。
I want to transfer all the data from array a to u32.



u32 =(a [1]<< 8)+ a [0];


(其中` 8`应该拼写为CHAR_BIT和1

和0可能需要互换,具体取决于你为字节选择的
字节顺序。)


u32 = (a[1] << 8) + a[0];

(where `8` should likely be spelled `CHAR_BIT` and `1`
and `0` may need interchanging depending on what
endianness you''ve picked for the bytes.)



如果int的大小也是char的大小,那么AFAIK就不会工作。 (*如果* 8是

替换为CHAR_BIT)

AFAIK that won''t work if int''s size is char''s size as well. (*if* 8 is
replaced with CHAR_BIT)


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

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