2011-04-07 87 views
0

嗨我碰到這一點的C + +代碼,並試圖瞭解如何指針操作。學習指針通過示例

int main() 
{ 
    int firstvalue, secondvalue; 
    int * mypointer; 

    mypointer = &firstvalue; 
    *mypointer = 10; 
    mypointer = &secondvalue; 
    *mypointer = 20; 
    cout << "firstvalue is " << firstvalue << endl; 
    cout << "secondvalue is " << secondvalue << endl; 
    return 0; 
} 

我的問題是如下:

  1. int *mypointer;聲明到底發生了什麼記憶?
  2. mypointer代表什麼?
  3. *mypointer代表什麼?
  4. *mypointer = 10;在內存中發生了什麼?
+2

發佈新的問題,請搜索。這個問題的答案應該告訴你一切你需要知道的指針:[理解C++指針](http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-什麼是可以做到克服) – 2011-04-07 05:22:09

+0

我看到那篇文章,但仍然困惑,所以我想我會嘗試召喚出另一個例子,並試圖理解它。 – locoboy 2011-04-07 05:25:26

回答

3
  1. 究竟發生在內存什麼時int * mypointer;被宣佈?

從棧中分配足夠的內存來存儲內存地址。這將是32或64位,具體取決於您的操作系統。

  1. mypointer代表什麼?

mypointer是包含存儲器地址的堆棧上的變量。

  1. * mypointer代表什麼?

*mypointer是由mypointer指向的實際存儲位置。

  1. 當* mypointer = 10;記憶中發生了什麼?

10存儲在mypointer指向的內存位置。例如,如果mypointer包含存儲器地址0x00004000,則值10被存儲在存儲器中的該位置處。

你有意見例如:

int main() 
{ 
    int firstvalue, secondvalue; // declares two integer variables on the stack 
    int * mypointer;    // declares a pointer-to-int variable on the stack 

    mypointer = &firstvalue;  // sets mypointer to the address of firstvalue 
    *mypointer = 10;    // sets the location pointed to by mypointer to 10. 
            In this case same as firstvalue = 10; because 
            mypointer contains the address of firstvalue 
    mypointer = &secondvalue;  // sets mypointer to the address of secondvalue 
    *mypointer = 20;    // sets the location pointed to by mypointer to 10. 
            In this case same as secondvalue = 20; because 
            mypointer contains the address of secondvalue 
    cout << "firstvalue is " << firstvalue << endl; 
    cout << "secondvalue is " << secondvalue << endl; 
    return 0; 
} 

試試這個代碼,看看是否有幫助:

int main() 
{ 
    int firstvalue, secondvalue; 
    int * mypointer;    

    cout << "firstvalue is " << firstvalue << endl; 
    cout << "secondvalue is " << secondvalue << endl; 
    cout << "mypointer is pointing to " << mypointer << endl; 

    mypointer = &firstvalue;  

    cout << "firstvalue is " << firstvalue << endl; 
    cout << "secondvalue is " << secondvalue << endl; 
    cout << "mypointer is pointing to " << mypointer << endl; 

    *mypointer = 10;    

    cout << "firstvalue is " << firstvalue << endl; 
    cout << "secondvalue is " << secondvalue << endl; 
    cout << "mypointer is pointing to " << mypointer << endl; 

    mypointer = &secondvalue;  

    cout << "firstvalue is " << firstvalue << endl; 
    cout << "secondvalue is " << secondvalue << endl; 
    cout << "mypointer is pointing to " << mypointer << endl; 

    *mypointer = 20;    

    cout << "firstvalue is " << firstvalue << endl; 
    cout << "secondvalue is " << secondvalue << endl; 
    cout << "mypointer is pointing to " << mypointer << endl; 

    return 0; 
} 
+0

所以如果我要cout'mypointer'會返回10存儲的地址嗎? – locoboy 2011-04-07 05:30:21

+1

@ cfarm54 - 是的。 – Mahesh 2011-04-07 05:31:18

+0

你可以以不同的方式對'* mypointer = 10'進行評論。抱歉。我有點困惑,你是「設置mypointer指向的位置」。你的意思是你在'mypointer'的地址處設置了值? – locoboy 2011-04-07 05:34:48

1
  1. 這將創建一個指向內存中的位置。通過在int *處定義它,我們說當我們取消引用這個指針時,這些字節應該被翻譯爲一個int。
  2. myPointer指向內存中的位置。如果您自己訪問myPointer,它會爲您指定存儲其指向的數據的地址。
  3. * myPointer取消引用指向該指針所指向的值的點。
  4. * myPointer = 10:取值10並將其存儲在myPointer指向的內存中。
1

int * mypointer;
它是一個聲明..它告訴編譯器我們將使用mypointer作爲整型指針變量。

mypointer =&firstvalue;
這裏第一個值的地址存儲在mypointer中。

* mypointer = 10;
到現在值10存儲在地址位置的地址位置4000考慮mypointer點4000

其他任何疑問,請及時與我溝通

1

到底發生了什麼時,內存INT * mypointer;被宣佈?

mypointer被聲明爲保存int的地址。因此,保存整數地址所需的字節數被分配給mypointer

mypointer代表什麼?

它是一個變量,可以容納一個整型變量的地址。

* mypointer代表什麼?

它取消引用指針。獲取地址位置mypointer處的值。

當* mypointer = 10;記憶中發生了什麼?

指定值10在位置mypointer成立。

1
  1. 當你聲明int *mypointer它聲明一個指針類型(long),它可以包含一個整數的內存地址。
  2. mypointer代表一個地址爲一個整數,其方式與街道地址代表街道上的房屋非常相似。
  3. *mypointer取消引用地址並指向int的實際值。這與地址指向的實際房屋類似。
  4. 在內存中,更新mypointer指向的內存位置的值。
2

pointers

+0

所以唯一缺少的是如果我調用'* ptr'的權利? – locoboy 2011-04-07 06:18:06

+0

ya ............. – Aravindhan 2011-04-07 06:21:55

+0

怎麼樣一個** ptr到* ptr到ptr的圖片呢? – starcorn 2011-08-28 20:11:30