c - 分配一个指向整数的指针

标签 c pointers assignment-operator

我可以将指针分配给整型变量吗?像下面这样。

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

pointer = &array1[0];

addressOfArray = pointer;

这样可以吗?

最佳答案

并非没有显式转换,即

addressOfArray = (int) pointer;

还有这个警告:

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.

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

关于c - 分配一个指向整数的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10489027/

相关文章:

c - 使用链表将数据插入堆栈

c - 为结构变量赋值

c - 作业,链表冒泡排序

c - C中删除数组元素

c - 在C中,如何读取地址前的内存块

c - 在 C 中创建时初始化数组的指向地址

用于指向结构成员的指针的 C 预处理器宏

c++ - 我应该在赋值运算符中检查 NULL 吗?

c++ - 二叉搜索树赋值运算符(递归)