2017-06-23 58 views
0

我從我的老師今天得到這個功能:指針功能,我不明白(

int nums[] = { 9, 5, 4, 2, 8, 1, 3, }; 
int *p = nums; 
int tmp_num = *(p + 2); 

p = &nums[0];  
*p = *p + *(p + 1); 

++p;    
++(*p);   

*(p + 2) = 7; 

p = &nums[5] + 1; 
*p = 4;    

int size = sizeof nums/sizeof nums[0]; 
for (int i = 0; i < size; ++i) 
{ 
    cout << "nums[" << i << "] : " << nums[i]  << endl; 
} 

結果:

NUMS [0]:14個

NUMS [1] :6

NUMS [2]:4個

NUMS [3]:7

NUMS [4]:8個

NUMS [5]:1個

NUMS [6]:4

有人能解釋如何做功能工作?我真的不明白你怎麼能得到這些結果。謝謝!

+1

哪些功能是你在說什麼?我在代碼片段中看不到任何函數 –

+1

發佈的代碼是非常基本的C++代碼。如果你不明白他們做了什麼,[一本好的教科書](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)將是你最好的選擇。 –

回答

0
int nums[] = { 9, 5, 4, 2, 8, 1, 3, }; 
int *p = nums;    // p points to the beginning of the array 
int tmp_num = *(p + 2); // take the value from the third element of the array and store it in tmp_num 

p = &nums[0];    // point p to the beginning of the array (again, unnessecary) 
*p = *p + *(p + 1);  // Add first element (*p) and second element (*(p+1)) and store the result in the first element (first element 9 becomes 14) 

++p;      // increment pointer -> point to next address. As it's currently still pointing to the first element, p now points to the second element 
++(*p);     // increment the value p is pointing to. 5 becomes 6. 

基於此,您應該能夠自己計算出其餘部分。 R Sahu是對的。這是基本的東西,你應該嘗試一本好書!

0

這是非常基本的東西。然而,與mattideluxe's answer一起,這裏詳細介紹了正在發生的事情,希望你能快速回復。

int nums[] = { 9, 5, 4, 2, 8, 1, 3, }; 
int *p = nums; // p is a pointer, which points to the first element (which is 9) 
int tmp_num = *(p + 2); // p + 2 is another pointer, pointing to the third element (4) 

p = &nums[0]; // again, p is assigned to pointing to the first element (which is 9) 
// the above line is a duplication of "p = nums;" (second line) 
*p = *p + *(p + 1); // the value pointed by p increments by the value pointed by (p + 1) 
// the above line's effect, nums[] = {9 + 5, 5, 4, 2, 8, 1, 3} 
// which is {14, 5, 4, 2, 8, 1, 3} 

++p; // pointer p shift to the right by one position 
// right now p points to the second element (which is 5)  
++(*p); // the second element increments by one 
// now nums is {14, 6, 4, 2, 8, 1, 3} 

*(p + 2) = 7; // the 4th element is assigned to 7 
// now nums is {14, 6, 4, 7, 8, 1, 3} 

p = &nums[5] + 1; // now pointer p points to the very last element 
*p = 4; 
// now nums is {14, 6, 4, 7, 8, 1, 4} 
1

這說明逐行可以定位你什麼是以往任何時候都行做

//nums is an array of ints but is also the address of the 1st element in the array 
int nums[] = { 9, 5, 4, 2, 8, 1, 3 }; 

// p is a pointer to an int initialized to nums 
// (initialized then to the address of the 1st e. in the array) 
int *p = nums; 

// p +2 means increase in the size of 2 integers the address of P 
// meaning now, p points to the 3rd element 
// *(p + 2) means I dereference that pointer and get the value of that address (4) 
int tmp_num = *(p + 2); 

// p gets now the address (&) of the 1st element of the array nums(the address of 9) 
p = &nums[0]; 
// the value at that position is now added to the value of the next int 9+5, 
// array is now: { 14, 5, 4, 2, 8, 1, 3 }; 
*p = *p + *(p + 1); 

// since p is a pointer p++ does increase by the size of one integer the address, 
// now p is pointing to element2 in the array ++p; 
// dereference the pointer and now increase the value by 1. i.e. 5+1, 
    array is now: { 14, 6, 4, 2, 8, 1, 3 }; 
++(*p); 
// add 7 to element 4 of the array... new val is 7, array is now: { 14, 6, 4, 7, 8, 1, 3 }; 
*(p + 2) = 7; 
// p is now holding the address of the last element in the array 
p = &nums[5] + 1; 
// we set that value to 4, array is now: { 14, 6, 4, 7, 8, 1, 4 }; 
*p = 4; 

int size = sizeof nums/sizeof nums[0]; 
for (int i = 0; i < size; ++i) 
{ 
    std::cout << "nums[" << i << "] : " << nums[i] << std::endl; 
} 

這只是因爲你應該已經知道指針是什麼,如何取消對它的引用,以及如何都將置值...

0

繪製數組和指針在一張紙上,並在跟蹤代碼時更新繪圖。

事情是這樣的:

 nums : | 9 | 5 | 4 | 2 | 8 | 1 | 3 | 
------------------------------------  
p = &nums[0]; 

        p+1 
        v 
     nums : | 9 | 5 | 4 | 2 | 8 | 1 | 3 | 
       ^
       p 

*p = *p + *(p + 1); 
[*p is 9; *(p+1) is 5; *p + *(p+1) is 14, so *p = 14] 

     nums : | 14 | 5 | 4 | 2 | 8 | 1 | 3 | 
       ^
       p 
-------------------------------------- 
++p; 
     nums : | 14 | 5 | 4 | 2 | 8 | 1 | 3 | 
        ^
         p 
--------------------- 
++(*p); 
     nums : | 14 | 6 | 4 | 2 | 8 | 1 | 3 | 
        ^  
         p  
----------------- 
*(p + 2) = 7; 

     nums : | 14 | 6 | 4 | 7 | 8 | 1 | 3 | 
        ^ ^
         p  p+2 
---------------- 
p = &nums[5] + 1; 

     nums : | 14 | 6 | 4 | 7 | 8 | 1 | 3 | 
             ^
              p 
---------------- 
*p = 4; 

     nums : | 14 | 6 | 4 | 7 | 8 | 1 | 4 | 
             ^
              p