查询C程序的输出

阿伦

我已经执行了一个程序并找到了输出。但是我对该程序的工作不满意。

代码的o / p:

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

我将以下代码与查询一起放入代码中。

#include <stdio.h>
#include <conio.h>
int  main( ) 
{ 
    static int a[ ] = {0,1,2,3,4}; 
    int *p[ ] = {a,a+1,a+2,a+3,a+4};
    int **ptr = p;
    ptr++;                        
    printf("%d",*ptr); //It displays me the output for the this line as -170 why?
    printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); 
    *ptr++; 
    printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); 
    *++ptr; 
    printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); 
    ++*ptr; 
    printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); 
} 
没有人

您在a[1]此处打印地址,因为* ptr指向a [1]元素,因此

  printf("%u",*ptr); // prints the address of a[1] element
  printf("%u",&a[1]); // check this statement you will get the same values

要打印值,用另一个*喜欢printf("%d",**ptr);它输出a[1]值,1 考虑

   a[0] -> 0  a[1] -> 1 a[2] -> 2 a[3] -> 3 a[4] - > 4
   1000       1004      1008      2012      2016  -> Addresses

   p[0] -> 1000  p[1] -> 1004  p[2] -> 1008  p[3] -> 2012  p[4] -> 2016
   4000          4004          4008          4012          4016 -> Addresses

   ptr - > 4000
   8000 -> Address

这就是您前3个阶段中发生的事情。

ptr++ ==> ptr = ptr + 4 ; ptr = 4000 + 4 -> ptr = 4004

so printf("%u %u %u %u",&ptr,ptr,*ptr,**ptr);

&ptr prints address of ptr    8000
 ptr prints address of p[1]   4000
*ptr prints value in p[1]     1004 (value in p[1] is address of a[1])
**ptr prints value of a[1]      1 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章