简体   繁体   English

请解释输出?

[英]Please Explain the Output?

can anyone explain the strage output of the program I know that the value has nothing to do with the value stored in the array but with the pointer thing but how is the second value coming to be 5: 任何人都可以解释程序的strage输出我知道该值与存储在数组中的值无关,但与指针有关,但第二个值是如何变为5:

int main()
{
    int **h;
    int a[2][2]={1,2,3,4};
    h=(int **)a;
    int i,j;
    printf("%d",*h);
    (*h)++;
    printf("\n%d",*h);

    getch();
    return 0;
}

What's happening is that *h is of type int* which is a pointer. 发生的事情是*hint*类型,它是一个指针。

When you increment it will actually increment by 4 rather than 1. Therefore the number you print out in the end is 1 + 4 = 5 . 当你增加它时实际上会增加4而不是1.因此你最后打印的数字是1 + 4 = 5

Here's your code with more prints: 这是你的代码有更多的打印:

  int **h; 
  int a[2][2]={1,2,3,4}; 
  h=(int **)a; 

  cout << h[0] << endl;
  cout << h[1] << endl;
  cout << h[2] << endl;
  cout << h[3] << endl;

  int i,j; 
  printf("%d",*h); 
  (*h)++; 
  printf("\n%d",*h); 

  cout << endl;
  cout << h[0] << endl;
  cout << h[1] << endl;
  cout << h[2] << endl;
  cout << h[3] << endl;

The output is: 输出是:

00000001
00000002
00000003
00000004
1
5
00000005
00000002
00000003
00000004

So you can see the first value, being incremented by 4. Because 4 is the size of the pointer when compiled for 32-bit. 因此,您可以看到第一个值,增加4.因为4是编译为32位时指针的大小。

By the statement h=(int **)a; 通过声明h=(int **)a; you have only allocated the memory address(the first) of the array a to h . 你只分配了数组ah的内存地址(第一个)。 You have also defined h as a pointer to a pointer which can also point to a two-dimensional array as you have done. 您还将h定义为指向指针的指针指针也可以指向二维数组。 Also, to be seen, you have not made h a two-dimensional array (using malloc recursively). 另外,为了可以看出,还没有作出h一个二维阵列 (使用malloc递归地)。 By printf("%d",*h); 通过printf("%d",*h); , you are trying to access the value at the address stored in h . ,您正尝试访问存储在h中的地址的值。

Arrays store values in memory in two ways, either column-wise or row-wise . 数组以两种方式将值存储在内存中,无论是按列还是按行 In either ways the memory locations are sequential, in your case also. 无论哪种方式,内存位置都是顺序的,在您的情况下也是如此。 So, h stores the memory address of the first element of the array a . 因此, h存储数组a的第一个元素的内存地址。 Therefore, when you use *h it retrieves the value at the address stored in h , ie, the first value in array a . 因此,当您使用*h它会检索存储在h中的地址的值,即数组a的第一个值。 And when you increment *h by (*h)++ , it increments because *h is still a pointer, and as you know incrementing a pointer doesn't mean incrementing by 1 , it actually increments by 4 . 而当你增加*h(*h)++ ,它增加因为*h仍然是一个指针,如你所知递增一个指针并不意味着通过递增1实际上,递增4

Hence, you are getting the above-mentioned output of yours. 因此,您获得了上述的输出。

Further discussions are welcomed from you Ankit . Ankit欢迎您进一步讨论。

- Sandip - Sandip

Since the expression *h is a pointer type, pointer arithmetic comes into play. 由于表达式*h指针类型,因此指针算法起作用。 Remember that pointer arithmetic takes the size of the base type into account; 请记住,指针算法会考虑基类型的大小; for any pointer of type T *p , the expression p++ will advance the pointer p by sizeof T bytes. 对于任何类型为T *p指针,表达式p++将使指针p前进一个sizeof T字节的sizeof T

Since *h is a pointer initialized to the value 1, the expression (*h)++ is read as "add sizeof (int *) bytes to 1", which in your case is obviously 4. Hence the output of 5. 由于*h是一个初始化为值1的指针,因此表达式(*h)++被读作“add sizeof (int *) bytes to 1”,在你的情况下显然为4.因此输出为5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM