简体   繁体   English

如何获得这个 C 程序的输出?

[英]How to get the output of this C program?

#include <stdio.h>
int main () {
int a[3][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int *p[] = {*a, *(a + 1) , *(a + 2) };
int (*ptr)[5];
ptr = a;
printf ("%d\n", *(*(p + 1) + 1));
printf ("%d %d %d\n", ** ptr , *(*( ptr + 1) + 2) , *(*( ptr + 2) + 3));
printf ("%d %d %d\n", ptr [0][0] , ptr [1][2] , ptr [2][3]) ;
return 0;
}

The output is输出是

7
1 8 14
1 8 14

Can someone explain this program step by step?有人可以一步一步解释这个程序吗? Really confused...真的很纠结...

For example for this line:例如对于这一行:

printf ("%d\n", *(*(p + 1) + 1));

Look at that line:看看那一行:

int *p[] = {*a, *(a + 1) , *(a + 2) };

So p is pointer (array) to integer pointers with some address and value *a at initial index.所以p是指向整数指针的指针(数组),在初始索引处具有一些地址和值*a

Then (p + 1) is next address of p and its value *(p + 1) is pointer *(a + 1)然后(p + 1)p下一个地址,它的值*(p + 1)是指针*(a + 1)

Next, a is two dimension array and (a + 1) is array of addresses for values 6, 7, 8, 9, 10接着, a是二维阵列和(a + 1)是地址的数组值6, 7, 8, 9, 10

Therefore *(p + 1) is address for value 6 and *(p + 1) + 1 is address for value 7.因此, *(p + 1)是值 6 的地址, *(p + 1) + 1是值 7 的地址。

So we get: *(*(p + 1) + 1) is integer value from address for value 7, ie所以我们得到: *(*(p + 1) + 1)是来自地址为 7 的整数值,即

printf ("%d\n", *(*(p + 1) + 1));

returns 7返回7

Try to figure out the rest of code.尝试找出其余的代码。 Same method.同样的方法。

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

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