C++ 指针赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7062853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ pointer assignment
提问by Fusionmate
Why is 90
the output value of y
and q
? I just do p=q
. How come the value of q
is changed?
为什么90
产值y
和q
?我只是做p=q
。的值怎么q
变了?
int main()
{
int x;
int y;
int *p = &x;
int *q = &y;
x = 35;
y = 46;
p = q;
*p = 90;
cout << x << " " << y << endl;
cout << *p << " " << *q << endl;
cout << "Address of p = " << p << endl;
cout << "Address of q = " << q << endl;
return 0;
}
The output is:
输出是:
35 90
90 90
Address of p = 0xbffa83c0
Address of q = 0xbffa83c0
回答by Crashworks
I'd like to share a general technique that I used to learn how pointers work when I was starting out. If you apply it to your problem, you'll see the answer as plain as day.
我想分享一个通用技术,我在开始时用它来学习指针的工作原理。如果你把它应用到你的问题上,你会看到答案很简单。
Get a big sheet of graph paper and lay it lengthwise on the table in front of you. This is your computer's memory. Each box represents one byte. Pick a row, and place the number '100' below the box at far left. This is "the lowest address" of memory. (I chose 100 as an arbitrary number that isn't 0, you can choose another.) Number the boxes in ascending order from left to right.
拿一张大方格纸,把它纵向放在你面前的桌子上。这是你电脑的内存。每个框代表一个字节。选择一行,然后将数字“100”放在最左侧的框下方。这是内存的“最低地址”。(我选择了 100 作为不是 0 的任意数字,您可以选择另一个。)从左到右按升序对框进行编号。
+---+---+---+---+---+-- | | | | | | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
Now, just for the moment, pretend an int is one byte in size. You are an eight-bit computer. Write your int a
into one of the boxes. The number below the box is its address. Now choose another box to contain int *b = &a
. int *b
is also a variable stored somewhere in memory, and it is a pointer that contains &a
, which is pronounced "a's address".
现在,暂时假设 int 的大小为一个字节。你是一台八位计算机。把你的int a
写进一个盒子里。框下方的数字是其地址。现在选择另一个盒子来包含int *b = &a
。int *b
也是一个存储在内存某处的变量,它是一个包含 的指针,&a
读作“a的地址”。
int a = 5;
int *b = &a;
a b +---+---+---+---+---+-- | 5 | |100| | | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
Now you can use this model to visually work through any other combinations of values and pointers that you see. It is a simplification (because as language pedants will say, a pointer isn't necessarilyan address, and memory isn't necessarilysequential, and there's stack and heap and registers and so on), but it's a pretty good analogy for 99% of computers and microcontrollers.
现在,您可以使用此模型直观地处理您看到的任何其他值和指针组合。这是一种简化(因为语言学究会说,指针不一定是地址,内存不一定是顺序的,还有堆栈、堆和寄存器等),但对于 99% 来说这是一个很好的类比计算机和微控制器。
So in your case,
所以在你的情况下,
int x = 35;
int y = 46;
x y +---+---+---+---+---+-- | 35| 46| | | | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
int *p = &x;
int *q = &y;
x y p q +---+---+---+---+---+-- | 35| 46|100|101| | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
p = q;
x y p q +---+---+---+---+---+-- | 35| 46|101|101| | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
*p = 90;
x y p q +---+---+---+---+---+-- | 35| 90|101|101| | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
Now what is *p
? What is *q
?
现在是什么*p
?什么是*q
?
回答by Nawaz
Because q
is the address of y
. And after p=q
, p also becomes the address of y
. That is why p
and q
print the same address when you print them using cout
.
因为q
是 的地址y
。之后p=q
,p 也变成了 的地址y
。这就是为什么p
和q
当你打印出来使用打印相同的地址cout
。
In other words, both p
and q
point to the same variable y
. So if you change the value of any of y
, *p
or *q
, then the change will occur in all, because all of them are same!
换句话说,p
和 都q
指向同一个变量y
。因此,如果您更改y
,*p
或 中的任何一个的值*q
,那么所有更改都会发生,因为它们都是相同的!
回答by Gaim
Well let's look at it after each step:
好吧,让我们在每一步之后看看它:
int x;
int y;
Now we have two variables x
and y
:
现在我们有两个变量x
和y
:
int *p = &x;
int *q = &y;
There are declared another two variables, pointer p
which points to variable x
and contains its address and pointer q
which points to variable y
and contains its address:
有声明的其他两个变量,指针p
指向的变量x
,并包含它的地址和指针q
指向变量y
和包含其地址:
x = 35;
y = 46;
Here you assign values to the variables, this is clear:
在这里,您为变量赋值,这很清楚:
p = q;
Now you assign address stored in q
to variable p
so both variables points to address in q
what is address of y
:
现在您将存储在的地址分配q
给变量,p
因此两个变量都指向地址的q
地址y
:
*p = 90;
Here you dereference p
, that is variable on address in p
and it is y
and you assign value 90
to variable y
.
在这里,你解引用p
,是可变的地址p
,它是y
与分配值90
变量y
。
回答by Vlad
The valueof q
didn't change, q
still points to y
. However, p
points to y
too after p = q
, so *p
is essentially y
, and *p = 90
assigns to y
.
该值的q
没有改变,q
仍然指向y
。但是,也p
指向y
之后p = q
,*p
本质上也是如此y
,并*p = 90
赋值给y
。
Note that cout << "Address of p = " << p << endl;
is misleading: p
and address of p
are two different beasts.
请注意,这cout << "Address of p = " << p << endl;
是一种误导:p
和地址p
是两种不同的野兽。
So your code runs like this:
所以你的代码是这样运行的:
int main() {
int x;
int y;
int *p = &x; // now p points to x
int *q = &y; // now q points to y
x = 35;
y = 46;
p = q; // now p is the same as q, i.e., points to y
*p = 90; // p points to y, so *p is y.
// so we assign 90 to y
cout << x << " " << y << endl;
cout << *p << " " << *q << endl; // both *p and *q are y
cout << "Address of p = " << p << endl;
cout << "Address of q = " << q << endl;
return 0;
}
回答by Ronald
See anotations:
见注释:
int main()
{
int x;
int y;
int *p = &x;
int *q = &y;
x = 35;
y = 46;
p = q; // at this point p is now pointing to the same memory address
// as q, both of them are pointing to the memory allocated to y
*p = 90; // this will also change the values in *q and y
cout << x << " " << y << endl;
cout << *p << " " << *q << endl;
cout << "Address of p = " << p << endl;
cout << "Address of q = " << q << endl;
return 0;
}
回答by Eric
after executing 'p = q;' statement, the two pointer point to the same variant 'y'. So when executing '*p = 90;', value of the variant 'y' is changed.
执行 'p = q;' 后 语句中,两个指针指向同一个变体'y'。因此,当执行 '*p = 90;' 时,变量 'y' 的值会发生变化。
回答by Ed Heal
int x;int y;int *p = &x;int *q = &y;x = 35;y = 46;
I.e p points to x (35) and q points to y (46)
即 p 指向 x (35) 而 q 指向 y (46)
p = q;
Now p points to y (46)
现在 p 指向 y (46)
*p = 90;
Now the contents of p (aka y) = 90
现在 p (又名 y) = 90 的内容
Now x = 35, y = 90, p and q point to y
现在 x = 35, y = 90, p 和 q 指向 y
cout << x << " " << y << endl;
Prints x, y i.e. 35 and 90
打印 x, y 即 35 和 90
cout << *p << " " << *q << endl;
p and q point to the same thing - y - whose value is 90 hence 90 and 90 is output
p 和 q 指向同一事物 - y - 其值为 90,因此输出 90 和 90
cout << "Address of p = " << p << endl;cout << "Address of q = " << q << endl;
As p and q are the same address will output the same value.
由于 p 和 q 是相同的地址将输出相同的值。
回答by Saeed
You first define p as a pointer which points to x. And then define q as a pointer pointing y. Then, you wrote p=q, so now, p and q both will point to y.
您首先将 p 定义为指向 x 的指针。然后将 q 定义为指向 y 的指针。然后,你写了 p=q,所以现在,p 和 q 都指向 y。
OK, changing *p, means changing y. then you assign 90 to y by the line *p=90;
好的,改变*p,意味着改变y。然后你通过行 *p=90; 将 90 分配给 y;
Now, you have this:
现在,你有这个:
- y : 90
- p points to y
- q points to y
- *p : 90
- *q : 90
- y : 90
- p 指向 y
- q 指向 y
- * p : 90
- * q : 90
回答by Shamim Hafiz
When you set p=q
, they both reference the same memory location. Therefore, if you change the value pointed to by p
, it will also change the value pointed to by q
, which is the address of y
. Therefore, the output of y
, *p
and *q
are same.
设置时p=q
,它们都引用相同的内存位置。所以,如果你改变了指向的值p
,它指向的值也会改变q
,也就是指向的地址y
。因此,输出y
,*p
和*q
是相同的。