C++ 我不懂的指针函数;(
今天我从老师那里得到了这个函数:C++ 我不懂的指针函数;(,c++,pointers,C++,Pointers,今天我从老师那里得到了这个函数: 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
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
有人能解释一下这个函数是如何工作的吗?我真的不明白你是如何得到这些结果的。谢谢
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是对的。这是基本的东西,你应该尝试一本好书!这确实是基本的东西。不过,下面是正在发生的事情的详细介绍,希望你能尽快了解
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}
这一行一行的描述可以让你了解每一行在做什么
//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;
}
这只是因为您应该已经知道指针是什么,如何取消引用它以及如何设置它的值…在一张纸上绘制数组和指针,并在跟踪代码时更新图形 大概是这样的:
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
你在说什么功能?我看不到SnpPET中的任何功能。发布的代码是非常基础的C++代码。如果你不理解他们的行为,那将是你最好的选择。