指针转换和数据类型转换 [英] Pointer conversions and Data types conversions

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

问题描述

大家好,

我是C的新手,我想知道所有指针转换是什么

" legal"根据ANSI C标准。例如,int *到char *,

some_struct *到char *等等..

据我所知,因为任何指针都可以转换为void *和void *可以将
强制转换为任何其他指针,这意味着任何指针都可以被强制转换为任何其他指针。是这样吗?

Hi all,
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer so this implies that any pointer can be
cast to any other pointer. Is that so?

推荐答案

vb@gmail.com 写道:

大家好,
我是C的新手,我想知道所有指针转换是什么
合法根据ANSI C标准。例如,int * to char *,
some_struct * to char *等...
根据我的说法,因为任何指针都可以转换为void *而void *可以转换为任何其他指针,这意味着任何指针都可以转换为任何其他指针。是这样吗?

Hi all,
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer so this implies that any pointer can be
cast to any other pointer. Is that so?




No.

指针可以转换成指向void的指针,

和转换回原始类型和价值。

保证转换指向无效的其他内容。


-

pete



No.
A pointer can be converted to a pointer to void,
and converted back to the original type and value.
Little else is guaranteed about conversions to pointer to void.

--
pete



vb@gmail.com 写道:
大家好,
我是C的新手,我想知道所有指针转换是什么
合法根据ANSI C标准。例如,int * to char *,
some_struct * to char *等...
根据我的说法,因为任何指针都可以转换为void *而void *可以转换为任何其他指针,这意味着任何指针都可以转换为任何其他指针。是这样吗?
Hi all,
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer so this implies that any pointer can be
cast to any other pointer. Is that so?



每个指针都是一个内存地址。大多数指针都是整数

而有些指针不是(这在comp中已经提到过了) .lang.c。)。

指针的种类会告诉编译器它指向了多少字节和数据类型

。所以我们可以投一个指针从一种类型到另一种类型,但它没有安全,也许你得到一些警告!你应该小心

足以使用''void *''指针,虽然它非常有用。


Every pointer is an address of memory.Most of the pointers are integers
and a few are not (This has been mentioned in comp.lang.c.).The kind of
pointer will tell the compiler how many bytes and what type of the data
it points to.So we can cast a pointer from one type to another,but it
is not safe and maybe you get some warnings!You should be careful
enough to use a ''void*'' pointer,though it is very useful.


vb@gmail.com 写道:
vb@gmail.com wrote:
大家好,
我是C的新手,我想知道所有的指针转换是什么
合法的根据ANSI C标准。例如,int * to char *,
some_struct * to char *等...
根据我的说法,因为任何指针都可以转换为void *而void *可以转换为任何其他指针


强制转换是显式转换,实际上是一个副问题。你是什​​么

真正在谈论的是一般的转换,不一定是明确的

转换。


实际上,保证在标准相当于:对于任何对象

类型T,进行以下转换:


T * tp =& SomeObjectOrOtherOfTypeT;

void * vp = tp;

T * ntp = vp;


不会丢失信息。也就是说,在执行此代码后,tp和ntp

将指向同一个对象。


如果你傻到这么做:


T * tp =& SomeTOrOther;

void * vp = tp;

U * up = vp;


然后你在更薄的冰上滑冰。例如,指针指向T和

指针指向U可能会有不同数量的有效位。


所以这意味着任何指针都可以
强制转换为任何其他指针。是这样吗?
Hi all,
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer
Casts are explicit conversions, and actually a side-issue here. What you are
really talking about is conversions in general, not necessarily explicit
conversions.

In fact, the guarantee in the Standard amounts to this: that, for any object
type T, the following transformation:

T *tp = &SomeObjectOrOtherOfTypeT;
void *vp = tp;
T *ntp = vp;

does not lose information. That is, after this code is executed, tp and ntp
will point to the same object.

If you are silly enough to do this:

T *tp = &SomeTOrOther;
void *vp = tp;
U *up = vp;

then you are skating on much thinner ice. For example, pointers-to-T and
pointers-to-U might have different numbers of significant bits.

so this implies that any pointer can be
cast to any other pointer. Is that so?




是的,你可以滑冰的冰的厚度没有限制。

迟早,虽然,表面会破裂,你会去。聪明人

观察标志,而不是扣篮。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

邮件:rjh在上述域名



Yes, there is no limit to the thinness of the ice on which you can skate.
Sooner or later, though, the surface will crack, and in you''ll go. Wise men
observe the signs, rather than take the dunking.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain


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

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