分配指向整数的指针 [英] Assigning a pointer to an integer

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

问题描述

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

int *pointer;int array1[25];int addressOfArray;指针 = &array1[0];addressOfArray = 指针;

可以这样做吗?

解决方案

并非没有明确的强制转换,即

addressOfArray = (int) 指针;

还有一个警告:

<块引用>6.3.2.3 指针
...
6 任何指针类型都可以转换为整数类型.除先前规定外,结果是实现定义的.如果结果不能用整数类型表示,行为未定义.结果不必在任何整数的值范围内类型.

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

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

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

pointer = &array1[0];

addressOfArray = pointer;

Is it possible to do like this?

解决方案

Not without an explicit cast, i.e.

addressOfArray = (int) pointer;

There's also this caveat:

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天全站免登陆