为什么(类型*)指针不等于*(类型**)指针? [英] Why (type*)pointer isn't equal to *(type**)pointer?

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

问题描述

为什么(类型*)指针不等于*(类型**)指针,


在代码片段中,它显示:

(int *)==(int **),

(int *)!=(*(int **))。


打字改变地址?或者不要打字吗

什么?


1 int main(无效)

2 {

3 char ch;

4 int * p;

5 int * p2;

6 int * p3;



8 ch =''c'';

9 p =(int *)(& ch);

10 p2 = *(int **)(& ch);

11 p3 =(int **)(& ch);

12

13 printf("%c",* p);

14 printf("%c",* p2); / *错误参考* /

15 printf("%c",* p3);

16

17返回0 ;

18}

19

~

~


谢谢


lovecreatesbeauty

解决方案

lovecreatesbeauty写道:

为什么(类型*)指针不等于*(类型**)指针,

在代码片段中,它显示:
(int *)==(int **),
(int *)!=(*(int **))。

类型转换会改变地址吗?或者没有打字做什么?

1 int main(无效)
2 {
3 char ch;
4 int * p;
5 int * p2;
6 int * p3;
7
8 ch =''c'';
9 p =(int *)( & ch);
10 p2 = *(int **)(& ch);




您正在转向指向int的指针并取消引用

结果,这将是''c''和垃圾。


Ian


< blockquote> lovecreatesbeauty写道:

为什么(类型*)指针不等于*(类型**)指针,




好,主要原因是第一个没有取消引用

指针,但第二个确实如此。你不可能期望两个

表达式具有相同的值,如果一个取消引用指针

而另一个没有。


- Logan


是的,我可以抽象int的4个字节中的第一个字节。这种

类型的播放是正常的,经常发生,我想。


Ian写道:


你正在转向指向int的指针并取消引用
结果,这将是''c''和垃圾。

Ian



Why (type*)pointer isn''t equal to *(type**)pointer,

In the code snippet, it shows that:
(int *) == (int **) ,
(int *) != (*(int **)) .

Does type-casting change the address? or doesn''t type-casting do
anything?

1 int main(void)
2 {
3 char ch;
4 int *p;
5 int *p2;
6 int *p3;
7
8 ch = ''c'';
9 p = (int *)(&ch);
10 p2 = *(int **)(&ch);
11 p3 = (int **)(&ch);
12
13 printf("%c", *p);
14 printf("%c", *p2); /* error in de-reference */
15 printf("%c", *p3);
16
17 return 0;
18 }
19
~
~

Thank you

lovecreatesbeauty

解决方案

lovecreatesbeauty wrote:

Why (type*)pointer isn''t equal to *(type**)pointer,

In the code snippet, it shows that:
(int *) == (int **) ,
(int *) != (*(int **)) .

Does type-casting change the address? or doesn''t type-casting do
anything?

1 int main(void)
2 {
3 char ch;
4 int *p;
5 int *p2;
6 int *p3;
7
8 ch = ''c'';
9 p = (int *)(&ch);
10 p2 = *(int **)(&ch);



Your are casting to a pointer to pointer to int and dereferencing the
result, which will be ''c'' and garbage.

Ian


lovecreatesbeauty wrote:

Why (type*)pointer isn''t equal to *(type**)pointer,



Well, the main reason is that the first does not dereference the
pointer, but the second does. You can''t possibly expect two
expressions to have the same value if one dereferences a pointer
and the other one does not.

- Logan


Yes, I can abstract the first byte among the 4 bytes of the int. This
kind of type-casting is normal and occurs frequently, I think.

Ian wrote:


Your are casting to a pointer to pointer to int and dereferencing the
result, which will be ''c'' and garbage.

Ian




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

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