等同指针值正在更改指针引用
Equating pointer value is changing pointer reference
struct node
{
char *ptr = (char *)malloc(frames*sizeof(char));
}*start,*current;
然后我分配了等于 node
的内存来启动。
[...]//Assigned values to start node.
current = start;//Current points to start
node *temp = new node();//temp will point a newly created node
*temp = *current;// COPYING VALUES OF CURRENT TO TEMP
[...]
我想创建一个新节点,temp
指向它并将 current
的值(此处当前指向开始)复制到 temp。
但这会使临时点current
(这里start
)代替。沮丧。我哪里出错了?
*temp = *current
应该temp = current
.
可能有两种解决方案
- 将 *temp=*current 更改为 temp=current。这样做,您可以使用"temp"访问"current"的值,因为这两个指针现在引用相同的内存位置。警告,使用"当前"或"temp"更改值将导致两个指针中的值发生变化,因为它们引用相同的内存位置。
- 使用内存。它会将值从一个内存位置复制到另一个内存位置。这是参考。现在,您有两个独立的值副本。
相关文章:
- 如何使用基类指针引用派生类成员
- 将常量指针引用绑定到非常量指针
- 如果非动态变量被指针引用,何时超出范围?
- 转换指针引用的字符串
- 指针引用的生存期(以 C++为单位)
- 从 unique_ptr::get 发送指针作为指针引用进入函数
- 指针/引用的 CLion 格式
- 使用模板专用化来比较指针引用
- 如何保存指向抽象基类的指针/引用,但在 c++ 中仍然可以复制
- C++初始化指针/引用/复制细微差别
- 用数组或指针引用函数?
- 指针范围问题和返回类中封装的指针向量内的指针引用
- 强制转换为指针引用是否会导致未定义的行为
- 访问由 void 指针引用的结构的成员
- C 为什么当先前的步骤引发异常时,std :: shared_ptr的指针引用会被破坏
- 动态指针引用数组由三元运算符返回值,但有异常
- 悬空指向 int 和 char* 常量的指针/引用
- C++ 二维数组和指针引用
- 通过使用指针/引用,在C++中使用向量加快计算速度
- C++非类型模板模板到函数指针/引用