c++ - 指针之间的差异
问题描述
我知道代码是如何工作的。但不知道为什么?
指针之间有什么区别?
struct List{
int val;
List *next;
}
/// where is the different between fun1 and fun2 receiving the List
void fun1(List **head){}
void fun2(List *head){}
int main(){
List *head;
/// where is the different between fun1 and fun2 passing the List
fun1(&head);
fun2(head);
}
解决方案
两者之间的区别在于您引用的内存地址。
fun1(&head);
: 你访问的内存地址就是head
指针本身的内存地址。fun2(head);
:您访问的内存地址是head
指向的位置。
尝试以head
这种方式输出值:
void fun1(List **head){
cout << "Pointer address: " << head << endl;
cout << "Head points to: " << *head << endl;
}
void fun2(List *head){
cout << "Head points to: " << head << endl;
}
输出将类似于:
Pointer address: 0x7ffeec04e058
Head points to: 0x104f2a036
Head points to: 0x104f2a036
通过使用&
运算符,您正在访问变量的内存地址head
。
推荐阅读
- sql-server - 使用 DATEDIFF 获取最近六个月的结果
- javascript - 有没有办法在窗口加载事件中停止加载某些资源以提高站点性能?
- c# - Asp.net Core 中的 Ajax 助手标签
- c - 如何在函数中分配双指针的内存?
- vb.net - 如何使我的百分比有 2 个小数点?
- animation - 如何将 bvh 文件解析为用 OpenGL 制作的骨架模型?
- javascript - 如何验证ajax调用中的数据
- javascript - 将变量从自定义服务器传递到 NextJS 中的组件
- python - 为什么 Pandas 给出 AttributeError:“SeriesGroupBy”对象没有属性“pct”?
- python - 如何将制表符作为 tkinter simpledialog.askstring() 的输入?