说我有一些代码,我有一个变量,我分配一个指针指向a
:指针分配完毕后,你能分配一个变量名给指针指向的东西吗?
int a = 5;
int* ptr = &a;
然而,在此之后,我想给这个变量a
占据名称b
的存储位置。
int b = a; // doesn't work, because it only copies the value
&b = ptr; // doesn't work, not an assignable value
ptr = &b; // doesn't work, this moves the pointer to point to b
// instead of renaming the location that ptr already pointed to
这可能吗? (这样做没有什么好的理由 - 只是好奇而已。)
- 编辑:这个问题不是关于指针和引用的区别,而是如何通过使用它们来实现目标,因此不是“引用和指针有什么区别?”