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

编程入门 行业动态 更新时间:2024-10-26 18:25:54
本文介绍了为什么(类型*)指针不等于*(类型**)指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么(类型*)指针不等于*(类型**)指针, 在代码片段中,它显示: (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

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

更多推荐

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

本文发布于:2023-06-01 10:29:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/413014.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   类型   不等于

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!