铸造指针 [英] casting pointers

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

问题描述

我有这个问题:假设我们有两个不同类型的指针:


struct foo * foo;

char ** array;


因为可以合法地将任何指针转换为(void *),并且因为(void *)

可以分配给任何指针类型,是不是将一个指针类型指定给另一个指针类型时需要进行强制转换吗?即因为


foo =(void *)数组;


是合法的(或者是吗?),不是(无效*)只有线路噪音?


TIA

解决方案

2003年10月4日星期六16:36:48 UTC,rihad< ri *** @ mail.ru>写道:

我有这个问题:假设我们有两个不同类型的指针:

struct foo * foo;
char * * array;

因为一个人总是合法地将任何指针强制转换为(void *),并且因为(void *)
可以分配给任何指针类型,在分配时是否需要强制转换
一个指针类型到另一个?即自从

foo =(void *)数组;


否:foo =((void *)((void *)((void *)((void *)((void *)array)))));


或者更好的另一个演员阵容是必要的。

是合法的(或者是吗?),不是(无效*)只是线路噪音?




没有char数组不是struct foo。你可以将float *转换为unsigned

char - 但这比将一种类型的指针(> b $ b)套入一般类型的其他类型更有意义。


并且将指向类型的指针转​​换为指针void以将此指针转换为另一种类型使得它不再合法。因为当你将一个

指针强制转换为void时,你只能将指针_back_强制转换为之前的

类型。指向void的指针不是一个容器,

隐藏原始类型,直到你需要该类型为止。


-

Tschau / Bye

Herbert


rihad< ri***@mail.ru>写道:

我有这个问题:假设我们有两个不同类型的指针:

struct foo * foo;
char * * array;

因为一个人总是合法地将任何指针强制转换为(void *),并且因为(void *)
可以分配给任何指针类型,在分配时是否需要强制转换
一个指针类型到另一个?即因为

foo =(void *)array;




我不遵循你的逻辑,这似乎是

非sequitur。它是*因为*你可以合法地将任何指针转换为

(void *),然后可以隐式转换为

适当的类型,以上是正确的。这是由于

的事实,对于其他指针类型来说,*不能保证相同。

,(void *)不仅仅是噪音。


施法存在的理由非常充分:保护你免受意外的错误。标准可能刚刚决定将
总是转换(如果可能)到左手

运算符的类型=。我很高兴它没有。


-Micah


2003年10月4日星期六21:36 :48 +0500,rihad< ri *** @ mail.ru>写道:

struct foo * foo;
char ** array;
< snip> foo =(void *)数组;

是合法的(或者是吗?),是不是(void *)只是线路噪音?



这种结构接近愚蠢。问题是数组到foo的转换是否合理。该语言允许在指向不同对象的指针之间转换
,但是存在问题

的对齐,潜在的未定义行为问题,语法问题

违规和语义问题。我会仔细检查每个指针

转换,以确保它是所需要的并且使

确定它是明智的。


祝福,

Bob


Hi, I''ve this question: suppose we have two differently typed pointers:

struct foo *foo;
char **array;

Becase one can always legally cast any pointer to (void *), and becase (void *)
is assignable to any pointer type, is it ever necessary to cast when assigning
one pointer type to another? I.e. since

foo = (void *) array;

is legal (or is it?), isn''t the (void *) there just line noise?

TIA

解决方案

On Sat, 4 Oct 2003 16:36:48 UTC, rihad <ri***@mail.ru> wrote:

Hi, I''ve this question: suppose we have two differently typed pointers:

struct foo *foo;
char **array;

Becase one can always legally cast any pointer to (void *), and becase (void *)
is assignable to any pointer type, is it ever necessary to cast when assigning
one pointer type to another? I.e. since

foo = (void *) array;
No: foo = ((void*) ((void*) ((void*) ((void*) ((void*) array)))));

or better another thousend of casts would be neccessary.

is legal (or is it?), isn''t the (void *) there just line noise?



No a char array is NOT a struct foo. You can cast float* to unsigned
char - but that makes no more sense than casing a pointer of one type
to another type in general.

And casting a pointer to a type to a pointer void to cast this pointer
to another type makes it no more legal. Because ever when you casts a
pointer to void you''re only allowed to cast that pointer _back_ to the
type it was before. A pointer to void is nothing than a container that
hides the original type until you needs that type back.

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch wird jetzt ausgeliefert!


rihad <ri***@mail.ru> writes:

Hi, I''ve this question: suppose we have two differently typed pointers:

struct foo *foo;
char **array;

Becase one can always legally cast any pointer to (void *), and becase (void *)
is assignable to any pointer type, is it ever necessary to cast when assigning
one pointer type to another? I.e. since

foo = (void *) array;



I''m not following your logic, which appears to be
non-sequitur. It is *because* you can legally cast any pointer to
(void*), which can then be implicitly converted to the
appropriate type, that the above is correct. It is due to the
fact that the same is *not* guaranteed for other pointer types
that the (void*) isn''t just noise.

Casting exists for a very good reason: to protect you from
inadvertant mistakes. The standard could have just decided to
always convert (when possible) to the type of the left-hand
operator to =. I''m damn glad it didn''t.

-Micah


On Sat, 04 Oct 2003 21:36:48 +0500, rihad <ri***@mail.ru> wrote:

struct foo *foo;
char **array; <snip>foo = (void *) array;

is legal (or is it?), isn''t the (void *) there just line noise?



This construction approaches silliness. The issue is whether the
conversion of array to foo is reasonable. The language allows
conversion between pointers to different objects, but there are issues
of alignment, issues of potential undefined behavior, issues of syntax
violations, and issues of semantics. I would inspect each pointer
conversion carefully to make sure it is what is needed and to make
sure it is sensible.

Best wishes,

Bob


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

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