简体   繁体   English

带指针的C输出

[英]C Output With Pointers

I'm trying to understand the output of the following piece of code, how does it produces an output 我试图理解下面一段代码的输出,它是如何产生输出的

-2 1 2 -4 3 -2 1 2 -4 3

int main()
{
    int i, a[5] = {3, 1, 2, -2, -4};
    int *p = a;

    for(i = 0; i < 5; i++)
    {
        printf("%d ", *(p + *p));
        p += *p;
    }

    return 0;
}
a[0]   a[1]  a[2]  a[3]  a[4]   // array indexes
 3       1     2    -2    -4    // value
1001   1005   1009  1013  1017  // Consider it is memory location.

p == 1001; // p=a;

Incrementation will done like this. 增量将像这样完成。 p+ (value * sizeof(int) )

In first loop, 在第一个循环中,

1001 + 3 ; // *(p + *p); p== 1001 , *p == 3

So it will print the value placed in the 1013 that is equal to -2. 因此它将打印放在1013中的值等于-2。

After that you are doing this `p += *p;` Now `p` points to `1013`

Second loop, 第二个循环,

1013 + -2; // *(p + *p); p== 1013 , *p == -2

So it will print 1 (1005 ) . 所以它将打印1 (1005 ) After that assigning , now p points to 1005 . 在分配之后,现在p指向1005

Third loop, 第三循环,

1005 + 1 ; // *(p + *p ); p == 1005  , *p == 1

It will print 2(1009) . 它将打印2(1009) Now p changes into 1009 . 现在p变为1009

Fourth loop, 第四循环,

1009 + 2 ;  // *(p + *p ); p == 1003 , *p == 2

It will print -4(1017) and p changes into 1017 . 它将打印-4(1017)并将p更改为1017

At finally, 最后,

1017 + -4 ; // It becomes 1001.

So It will print the value in 1001. so the value is 3. 因此它将在1001中打印该值,因此值为3。

This program is mainly based on pointer arithmetic. 该程序主要基于指针算法。

  • p is the pointer. p是指针。
  • *p is the value at the pointer *p是指针的值
  • *(p + *p) is effectively p[*p] *(p + *p)实际上是p[*p]
  • p += *p; is p = p + *p p = p + *p

Now, based on the particular values, in this case, for first iteration, 现在,基于特定值,在这种情况下,对于第一次迭代,

printf("%d ", *(p + *p));

is

printf("%d ", *(p + 3));

or printf("%d ", p[3]); 或printf(“%d”,p [3]);

which gives -2. 给-2。

Next, p += *p is p += 3 and so on. 接下来, p += *pp += 3 ,依此类推。

You can read more about pointer arithmatic here . 您可以在此处阅读有关指针算术的更多信息。

*(p + *p) = *(p + 3) = p[3] = -2 /* p = a and *p will give the value of first element of the array */

p = p + *p = p + 3

*(p + *p) = *((p+3) + *(p+3)) = *((p+3) + p[3]) = *(p + 3 -2) = p[1] = 1

and so on 等等

PS: a[i] = *(a+i) PS: a[i] = *(a+i)

Consider this condition... n some byte address 考虑这种情况... n某个字节地址

 Elements:       3  1    2   -2      4
 with addresses:  n  n+1  n+2  n+3  n+4 (this is in bytes)

Now initially we have *p=a that is p points the first element of array a. 现在我们首先得到*p=a ,即p指向数组a的第一个元素。 So, we would be having *p=3 and p=n (ie p has address of first element which is n) . 因此,我们将得到*p=3p=n (即p具有第一个元素的地址,即n)。

In the first loop we have .. printf("%d ", *(p + *p)); 在第一个循环中,我们有.. printf("%d ", *(p + *p)); .... ....

So, *(p + *p) implies element at address (p+*p) where we have p=n and *p=3 . 因此, *(p + *p)表示地址(p + * p)处的元素,其中我们有p=n*p=3 So *(p + *p) is the element at n+3 that is -2 . 所以*(p + *p)n+3处的元素,即-2

No we have p += *p; 不,我们有p += *p; . Which means p=p+ *p . 这意味着p=p+ *p SO p = n + 3 ==> p now points to the -2 .. SO p = n + 3 ==> p现指向-2 ..

This is how it loops.. 这是它循环的方式..

Loop 1:
   *p = 3
    p = n
    *(p + *p ) ==> *(n+3) = -2
    p = p + *p ==>  p = n+3
Loop 2:
    *p = -2
     p = n+3
    *(p + *p ) ==> *(n+3 -2) = 1
     p =p + *p ==> p = n+3 -2 ==> n+1 
Loop 3:
    *p = 1
     p= n+1
    *(p + *p ) ==> *(n+1 +1) = 2
     p =p + *p ==> p = n+1 +1 ==> n+2
Loop 4:
    *p = 2
     p= n+2
    *(p + *p ) ==> *(n+2 +2) = -4
     p =p + *p ==> p = n+2 +2 ==> n+4
Loop 5:
    *p = -4
     p= n+4
    *(p + *p ) ==> *(n+4 -4) = 3
     p =p + *p ==> p = n+4 -4 ==> n
value  3   1    2   -2   -4
addr 100  104  108  112  116  (say)

First iteration:
------------------------------------------------------------------
int *p = a; --> p = 100 (base address of the array )
( *(p + *p) ) --> (*(100+4(size of int)X *(100) ) --> (*(100+4X3)) -->(*(112))= -2

 p += *p; -->(p= (p+*p))-->(100 +*(100)X4) --> (100 +3X4) --> (112)
Now p=112 (memory address).

Second iteration:
----------------------------------------------------------------------
( *(p + *p) )-->(*(112+*(112)X4)-->*(112 (-2X4))-->*(112-8)=*(104)=1
p += *p; -->(p= (p+*p))-->((112 + *(112)X4)-->(112 +(-2X4))= 104
now p=104 (memory address)


like this ......

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

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