I am just making sure I understand this concept correctly. With the * operator, I make a new variable, which is allocated a place in memory. So as to not unnecessarily duplicate variables and their values, the & operator is used in passing values to methods and such and it actually points to the original instance of the variable, as opposed to making new copies...Is that right? It is obviously a shallow understanding, but I just want to make sure I am not getting them mixed up. Thanks!
我只是确保我正确地理解了这个概念。使用*操作符,我创建一个新变量,它在内存中分配一个位置。为了避免不必要地重复变量及其值,&运算符用于将值传递给方法等,它实际上指向变量的原始实例,而不是创建新的副本……是这样吗?这显然是一个肤浅的理解,但我只是想确保我不会把他们弄混。谢谢!
3 个解决方案
#1
40
Not quite. You're confusing a *
appearing in a type-name (used to define a variable), with the *
operator.
不完全是。您将出现在类型名称(用于定义变量)中的*与*操作符混淆。
int main() {
int i; // i is an int
int *p; // this is a * in a type-name. It means p is a pointer-to-int
p = &i; // use & operator to get a pointer to i, assign that to p.
*p = 3; // use * operator to "dereference" p, meaning 3 is assigned to i.
}
#2
12
One uses &
to find the address of a variable. So if you have:
我们使用&查找变量的地址。所以如果你有:
int x = 42;
and (for example) the computer has stored x
at address location 5
, &x
would be 5
. Likewise you can store that address in a variable called a pointer:
(例如)计算机在地址位置5存储了x,而x则是5。同样,您可以将该地址存储在一个名为指针的变量中:
int* pointer_to_x = &x; // pointer_to_x has value 5
Once you have a pointer you can dereference it using the *
operator to convert it back into the type to which it points:
一旦你有了一个指针,你就可以使用*运算符将它还原为它指向的类型:
int y = *pointer_to_x; // y is assigned the value found at address "pointer_to_x"
// which is the address of x. x has value 42, so y will be 42.
#3
4
When a variable is paired with the * operator, that variable holds a memory address.
当一个变量与*运算符配对时,该变量保存一个内存地址。
When it is paired with the & operator, it returns the address at which the variable is held.
当它与&操作符配对时,它返回变量所在的地址。
If you had
如果你有
int x = 5; //5 is located in memory at, for example, 0xbffff804
int *y = &x; //&x is the same thing as 0xbffff804, so y now points to that address
both x
and *y
would yield 5
x和*y都会得到5
#1
40
Not quite. You're confusing a *
appearing in a type-name (used to define a variable), with the *
operator.
不完全是。您将出现在类型名称(用于定义变量)中的*与*操作符混淆。
int main() {
int i; // i is an int
int *p; // this is a * in a type-name. It means p is a pointer-to-int
p = &i; // use & operator to get a pointer to i, assign that to p.
*p = 3; // use * operator to "dereference" p, meaning 3 is assigned to i.
}
#2
12
One uses &
to find the address of a variable. So if you have:
我们使用&查找变量的地址。所以如果你有:
int x = 42;
and (for example) the computer has stored x
at address location 5
, &x
would be 5
. Likewise you can store that address in a variable called a pointer:
(例如)计算机在地址位置5存储了x,而x则是5。同样,您可以将该地址存储在一个名为指针的变量中:
int* pointer_to_x = &x; // pointer_to_x has value 5
Once you have a pointer you can dereference it using the *
operator to convert it back into the type to which it points:
一旦你有了一个指针,你就可以使用*运算符将它还原为它指向的类型:
int y = *pointer_to_x; // y is assigned the value found at address "pointer_to_x"
// which is the address of x. x has value 42, so y will be 42.
#3
4
When a variable is paired with the * operator, that variable holds a memory address.
当一个变量与*运算符配对时,该变量保存一个内存地址。
When it is paired with the & operator, it returns the address at which the variable is held.
当它与&操作符配对时,它返回变量所在的地址。
If you had
如果你有
int x = 5; //5 is located in memory at, for example, 0xbffff804
int *y = &x; //&x is the same thing as 0xbffff804, so y now points to that address
both x
and *y
would yield 5
x和*y都会得到5