整数指针转换为整数 [英] Conversion of integer pointer to integer

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

问题描述

尝试以下code,检查,当我们转换成整数指针整数会发生什么。

Tried following code to check what happens when we convert integer pointer to a integer .

 #include<stdio.h>
 #include<stdlib.h>
 int main()
 {
         int *p;
         int a;
         p = (int *)malloc(sizeof(int));
         *p = 10;
         a = (int)p;
         printf("%d\n",*p);
         printf("%d \n",a);
         return 0;
 }

 The output is : 10
                 135680008

谁能解释,与此相关的转换概念呢?提供与此相关的主题的任何链接也将是有益的。

Can anyone explain, the concept related to this conversion? Any links provided related to this topic would also be helpful.

推荐答案

显然,你混淆了终场指针的内容。

Apparently you confuse the pointer with the content of the pointer.

如果我们谈论现实世界中,你可以说,我在鸟的指点下,你想我的食指转换为一只鸟。但有类型'鸟'和'手指'。

If we were talking about the real world, you could say that, with me pointing at a bird, you want to convert my index finger to a bird. But there is no relation between the type 'bird' and 'finger'.

传输的类比程序:要转换指向你的 INT INT 本身的对象。由于C指针被称为存储单元的数量'实施,因为有大量的存储单元可用,很明显,(INT)P 将导致一个非常大的数字。

Transferring that analogy to your program: you are converting the object pointing to your int to an int itself. Since a C pointer is implemented as 'the number of a memory cell', and since there are lots of memory cells available, it's obvious that (int)p will result in a very big number.

铸造是一个讨厌的事情。这是一个巧合,指针是相当类似的整数。如果他们为实现第n 的地址m 记忆银行,你就不会问这个问题,因为那里不会有明显的关系,你不会已经能够做到这一点偏色。

Casting is a nasty thing. It's a coincidence that pointers are quite analogous to integers. If they were implemented as "the nth address of the mth memory bank", you wouldn't be asking this question because there wouldn't have been an obvious relation, and you wouldn't have been able to do this cast.

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

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