def*_*eta 5 language-features pointers programming-languages
在许多C风格的语言和一些像Fortran这样的旧语言中,可以使用指针.
作为一个只使用基本,javascript和actionscript编程的人,你能告诉我Pointer是什么,以及它最有用的是什么?
谢谢!
指针是包含另一个变量地址的变量.这允许您间接引用另一个变量.例如,在C中:
// x is an integer variable
int x = 5;
// xpointer is a variable that references (points to) integer variables
int *xpointer;
// We store the address (& operator) of x into xpointer.
xpointer = &x;
// We use the dereferencing operator (*) to say that we want to work with
// the variable that xpointer references
*xpointer = 7;
if (5 == x) {
// Not true
} else if (7 == x) {
// True since we used xpointer to modify x
}
Run Code Online (Sandbox Code Playgroud)
指针并不像听起来那么难。正如其他人已经说过的,它们是保存其他变量地址的变量。假设我想告诉你去我家的路线。我不会给你我房子的照片,或者我房子的比例模型;我只想给你地址。你可以从中推断出你需要什么。
同样,许多语言也区分按值传递和按引用传递。本质上,这意味着每次我需要引用它时,我都会传递整个对象吗?或者,我会直接给出它的地址,以便其他人可以推断他们需要什么吗?
大多数现代语言通过确定指针何时有用并为您进行优化来隐藏这种复杂性。但是,如果您知道自己在做什么,则手动指针管理在某些情况下仍然有用。
| 归档时间: |
|
| 查看次数: |
1102 次 |
| 最近记录: |