這是非常基本的東西。然而,與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}
來源
2017-06-23 06:12:00
xhg
哪些功能是你在說什麼?我在代碼片段中看不到任何函數 –
發佈的代碼是非常基本的C++代碼。如果你不明白他們做了什麼,[一本好的教科書](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)將是你最好的選擇。 –