指针
一句话解释
指针是 变量那个存储 memory 地址的 another 值, 而不是值 它自己.
用大白话说
A pointer is a variable that does not hold a regular value — it holds the ADDRESS of where a value lives in memory. Think of it like a forwarding address: instead of holding the person, it holds the location where you can find the person.
Pointers are explicit in low-level languages like C and C++, where you manually manage memory addresses. In higher-level languages like JavaScript and Python, pointers exist behind the scenes as "引用" — when you assign an object to a variable, the variable holds a reference (pointer) to the object, not a copy.
Understanding pointers/references is crucial for understanding why let = obj; let b = obj; makes a and b point to the SAME object — modifying through one affects the other. This is called "引用语义" and is a common source of bugs for beginners.
示例
// JavaScript uses references for objects
const obj1 = { count: 0 };
const obj2 = obj1; // obj2 points to the SAME object
obj2.count = 5;
console.log(obj1.count); // 5 — changed through obj2!
// Primitives are copied by value
let a = 5;
let b = a;
b = 10;
console.log(a); // 5 — unchangedobj1 and obj2 是引用 (指针) 到相同对象. 更改通过 one 是可见通过 other. Primitives 是复制通过值, 所以更改 b 做不 affect .
常见混淆
容易混淆的对象: 引用
关键区别: 指针是 explicit memory 地址 (C/C++); 引用是 抽象 handle managed 通过 language (JavaScript/Python/Java). 引用是 safer 但更少 flexible.
相关术语
相关资源
相关课程
- Memory and 引用
学习关于 memory and 引用.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。