我可以将指针分配给整型变量吗?像下面这样。
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/