Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 以下程序背后的逻辑是什么?_C++_C++11_Visual C++_Visual Studio 2015 - Fatal编程技术网

C++ 以下程序背后的逻辑是什么?

C++ 以下程序背后的逻辑是什么?,c++,c++11,visual-c++,visual-studio-2015,C++,C++11,Visual C++,Visual Studio 2015,我无法理解这个逻辑,请帮助我 您的cout相当于 std::cout我将在以下代码中以注释的形式解释逻辑: output is -1 #包括 使用名称空间std; int main() { int t[4]={8,4,2,1}; int*p1=t+2,*p2=p1-1;/*p1将指向数组的第三个元素,即t[2]=2,p2将指向第二个元素,即t[1]=4*/ p1++;//现在p1将指向第四个元素,即t[3]=1 cout请查看注释以寻求解决方案 #include <iostream&g

我无法理解这个逻辑,请帮助我

您的
cout
相当于


std::cout我将在以下代码中以注释的形式解释逻辑:

output is -1 
#包括
使用名称空间std;
int main()
{
int t[4]={8,4,2,1};
int*p1=t+2,*p2=p1-1;/*p1将指向数组的第三个元素,即t[2]=2,p2将指向第二个元素,即t[1]=4*/
p1++;//现在p1将指向第四个元素,即t[3]=1

cout请查看注释以寻求解决方案

#include <iostream>
using namespace std;
int main()
{
  int t[4] = { 8, 4, 2, 1 };
  int *p1 = t + 2, *p2 = p1 - 1; /* p1 will point to 3rd element of array i.e. t[2]=2 and p2 will point to 2nd element i.e. t[1]=4 */
  p1++; // now p1 will point to 4th element i.e t[3]=1 
  cout << *p1 - t[p1 - p2] << endl; /* So 1-t[2] = 1 - 2 = -1 Since array difference is return in terms of Number of element which can be occupied in that memory space . So, instead of returning 8 , p1-p2 will give 2 */ 
  return 0;
}
#包括
使用名称空间std;
int main()
{
int t[4]={8,4,2,1};
int*p1=t+2,*p2=p1-1;
//p1持有t[2]的地址,p2持有t[1]的地址
p1++;
//现在,p1持有t[3]的地址
库特
int t[4]={8,4,2,1}

int*p1=t+2

int*p2=p1-1

p1++

(p1-p2=>2)


cout指针算法将指针对象类型考虑在内。可能重复:@Bathsheba@Aakash Deep@Killzone Kid@S_Madankar.非常感谢你。Protips(1)只允许你标记一个人。是我,因为我是第一个如果答案告诉你你想知道的,你应该接受。投票表决,这是回答这个问题的一个很好的清晰方式。“注意,指针算术是以你类型的大小为单位的,而不是1”:在我看来,这是你答案中最重要的部分。
#include <iostream>
using namespace std;
int main()
{
  int t[4] = { 8, 4, 2, 1 };
  int *p1 = t + 2, *p2 = p1 - 1; /* p1 will point to 3rd element of array i.e. t[2]=2 and p2 will point to 2nd element i.e. t[1]=4 */
  p1++; // now p1 will point to 4th element i.e t[3]=1 
  cout << *p1 - t[p1 - p2] << endl; /* So 1-t[2] = 1 - 2 = -1 Since array difference is return in terms of Number of element which can be occupied in that memory space . So, instead of returning 8 , p1-p2 will give 2 */ 
  return 0;
}
#include <iostream>
using namespace std;
int main()
{
  int t[4] = { 8, 4, 2, 1 };

  int *p1 = t + 2, *p2 = p1 - 1;  
  // p1 holds the address of t[2],  p2 holds the address of t[1]  

  p1++;   
  //Now, p1 holds the address of t[3]  

  cout << *p1 - t[p1 - p2] << endl;  // We need to pointer arithmetic  
  //Here p1 has the value 1,
  //t[(t+3)-(t+1)] = t[2] which is 2,
  //So, the answer would be 1-2 = -1
  return 0;
}
{ 8, 4, 2, 1 }
  ^
  t
{ 8, 4, 2, 1 }
        ^
        p1
{ 8, 4, 2, 1 }
     ^  ^
     p2 p1
{ 8, 4, 2, 1 }
     ^     ^
     p2    p1
1 - t[2] => 1 - 2 => -1