将指针分配给整数 [英] Assigning a pointer to an integer

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

问题描述

我可以分配一个指向整数变量的指针吗?像下面这样.

Can I assign a pointer to an integer variable? Like the following.

int *pointer;
int array1[25];
int addressOfArray;

pointer = &array1[0];

addressOfArray = pointer;

有可能这样做吗?

推荐答案

并非没有显式强制转换,即

Not without an explicit cast, i.e.

addressOfArray = (int) pointer;

还有一个警告:

6.3.2.3指针
...
6任何指针类型都可以转换为整数类型.除先前指定的内容外, 结果是实现定义的.如果结果无法以整数类型表示, 行为是不确定的.结果不必在任何整数的值范围内 类型.
6.3.2.3 Pointers
...
6 Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

因此,不能保证结果是有意义的整数值.

So the result isn't guaranteed to be a meaningful integer value.

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

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