簡體   English   中英

C++ 指針賦值

[英]C++ pointer assignment

為什么yq的 output 值是90 我只是做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;
}

output 是:

35 90
90 90
Address of p = 0xbffa83c0
Address of q = 0xbffa83c0

我想分享一個通用技術,當我開始學習指針的工作原理時,我使用該技術。 如果你把它應用到你的問題上,你會看到答案一清二楚。

拿一張大方格紙,將它縱向放在你面前的桌子上。 這是您計算機的 memory。 每個框代表一個字節。 選擇一行,然后將數字“100”放在最左邊的框下方。 這是 memory 的“最低地址”。 (我選擇了 100 作為非 0 的任意數字,您可以選擇另一個。)從左到右按升序對框進行編號。

+---+---+---+---+---+--
|   |   |   |   |   | ...
+---+---+---+---+---+--
100  101 102 103 104  ...

現在,暫時假設一個 int 大小為一個字節。 你是一台八位計算機。 將您的int a寫入其中一個框中。 方框下方的數字是它的地址。 現在選擇另一個框來包含int *b = &a int *b也是存儲在 memory 某處的變量,它是一個包含&a的指針,讀作“a 的地址”。

int  a = 5;
int *b = &a;
  a       b 
+---+---+---+---+---+--
| 5 |   |100|   |   | ...
+---+---+---+---+---+--
 100 101 102 103 104  ...

現在,您可以使用此 model 直觀地處理您看到的任何其他值和指針組合。 這是一種簡化(因為正如語言學究所說,指針不一定是地址,memory不一定是順序的,還有堆棧、堆和寄存器等等),但對於 99% 來說,這是一個很好的類比計算機和微控制器。

所以在你的情況下,

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  ...

現在*p是什么? 什么是*q

因為qy的地址。 p=q之后, p 也成為y的地址。 這就是為什么pq在使用cout打印它們時會打印相同的地址。

換句話說, pq都指向同一個變量y 因此,如果您更改任何y*p*q的值,那么更改將全部發生,因為它們都是相同的!

好吧,讓我們在每一步之后看看它:

int x;
int y;

現在我們有兩個變量xy

int *p = &x;
int *q = &y;

聲明了另外兩個變量,指向變量x並包含其地址的指針p和指向變量y並包含其地址的指針q

x = 35;
y = 46;

在這里你為變量賦值,這很清楚:

p = q;

現在您將存儲在q中的地址分配給變量p ,因此兩個變量都指向q中的地址y的地址是什么:

*p = 90;

在這里,您取消引用p ,即p中地址上的變量,它是y ,您將值90分配給變量y

q沒有改變, q仍然指向y 但是, pp = q之后也指向y ,因此*p本質上是y ,並且*p = 90分配給y

注意cout << "Address of p = " << p << endl; 具有誤導性: pp的地址是兩種不同的野獸。

所以你的代碼像這樣運行:

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;
}

見注釋:

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;
}

執行 'p = q;' 后語句,這兩個指針指向同一個變體'y'。 所以當執行'*p = 90;'時,變量'y'的值會改變。

int x;int y;int *p = &x;int *q = &y;x = 35;y = 46;

即 p 指向 x (35) 和 q 指向 y (46)

p = q;

現在 p 指向 y (46)

*p = 90;

現在 p (aka y) = 90

現在 x = 35, y = 90, p 和 q 指向 y

cout << x << " " << y << endl;

打印 x、y 即 35 和 90

cout << *p << " " << *q << endl;

p 和 q 指向同一事物 - y - 其值為 90,因此 90 和 90 為 output

cout << "Address of p = " << p << endl;cout << "Address of q = " << q << endl;

由於 p 和 q 是相同的地址,所以 output 的值相同。

當您設置p=q時,它們都引用相同的 memory 位置。 因此,如果你改變p指向的值,它也會改變q指向的值,也就是y的地址。 因此, y*p*q的 output 是相同的。

您首先將 p 定義為指向 x 的指針。 然后將 q 定義為指向 y 的指針。 然后,你寫了 p=q,所以現在 p 和 q 都指向 y。

OK,改變*p,意味着改變y。 然后通過 *p=90; 行將 90 分配給 y;

現在,你有這個:

  • 是:90
  • p 指向 y
  • q 指向 y
  • *p:90
  • *q: 90

首先讓我解釋一下你的流程。 將 x 的引用放在 *p 中,將 y 的引用放在 *q 中,然后將值分配給 x 和 y。 現在你的缺陷從這里開始,假設 x 的地址是 abc,y 是 xyz,在你的示例上下文中,你將 q 分配給 p 表示 q 的值現在是 xyz,p 和 q 都指向 xyz,只要你將值 90 放在地址 xyz 上,所以它在 q 和 p 上都打印 90

int main() {

int x;
int y;
int *p = &x;//this p pointer stores address of variable x
int *q = &y;//this q pointer stores address of variable y

x = 35;//assigned value 35 to x
y = 46;//assigned value 46 to y

p = q;//this line make pointer p to store address which is their in q that is address of y

*p = 90;//this line will change the value of y because by previous we change the pointer p address before line p=q p was pointing to x but now it is pointing to y

cout << x << " " << y << endl;
cout << *p << " " << *q << endl;as we make pointers p and q pointing to same variable that is y and change the value of y to 90 by doing*p=90 output for both *p & *q is 90
cout << "Address of p = " << p << endl;
cout << "Address of q = " << q << endl;

return 0;

希望我能夠回答這個問題,如果喜歡然后請投票,以便更多人可以看到這個答案謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM