指针程序的输出[关闭](Output of a pointer program [closed])

编程入门 行业动态 更新时间:2024-10-25 14:27:28
指针程序的输出[关闭](Output of a pointer program [closed])

我对指针知之甚少。

我遇到了以下程序。 输出似乎正常,但实际上发生的事情我无法理解。

#include<stdio.h> #include<conio.h> void main() { int k; int a[] = {1,2,3}; int *b[3] ; int **c[3]; int ***d[3]; int ****e[3]; int*****f[3]; for (k = 0 ; k <3; k++) { b[k] = a + k; c[k] = b + k ; d[k] = c + k; e[k] = d + k ; f[k] = e + k; } for (k = 0 ; k <3; k++) { printf("%3d", *b[k]); printf("%3d", **c[k]); printf("%3d", ***d[k]); printf("%3d", ****e[k]); printf("%3d\n", *****f[k]); } }

I know little about pointers.

I came across the following program. The output seems normal but what is actually going going on I could not figure it out.

#include<stdio.h> #include<conio.h> void main() { int k; int a[] = {1,2,3}; int *b[3] ; int **c[3]; int ***d[3]; int ****e[3]; int*****f[3]; for (k = 0 ; k <3; k++) { b[k] = a + k; c[k] = b + k ; d[k] = c + k; e[k] = d + k ; f[k] = e + k; } for (k = 0 ; k <3; k++) { printf("%3d", *b[k]); printf("%3d", **c[k]); printf("%3d", ***d[k]); printf("%3d", ****e[k]); printf("%3d\n", *****f[k]); } }

最满意答案

第一个for循环只是基本的指针算法。 a[]保存int ,后面的每个数组都有一个指针。 b[]是指向int的指针 c[]是指向int指针 等等

所以它在内存中是这样的:

Memory Address: 0x00441234 <---+ 0x00441238 <----+ 0x0044123C <---+ ********** | ********** | ********** | var name: * a (+0) * | * a (+1) * | * a (+2) * | ********** | ********** | ********** | value: * 1 * | * 2 * | * 3 * | ********** | ********** | ********** | | | | | | | +-> 0x00442345 | +->0x00442349 | +->0x0044234D | | ************ | | ************ | | ************ | | * b (+0) * | | * b (+1) * | | * b (+2) * | | ************ | | ************ | | ************ | | *0x00441234* -+ | *0x00441238* --+ | *0x0044123C* --+ | ************ | ************ | ************ | | | | | | | 0x00443345 | 0x00443349 | 0x0044334D | ************ | ************ | ************ | * c (+0) * | * c (+1) * | * c (+2) * | ************ | ************ | ************ +-- *0x00442345* +-*0x00442349* +-*0x0044234D* ************ ************ ************

并且D每个元素指向C每个元素,依此类推。 最终的结果是你将每个数组中的每个元素(通过一些指针链)设置回a的元素。 然后在第二个for循环中,你一遍又一遍地打印a[]的元素。

The first for loop is just basic pointer arithmetic. a[] holds ints, each array after that holds a pointer. b[] is a pointer to int c[] is a pointer to pointer to int etc

So it's something like this in memory:

Memory Address: 0x00441234 <---+ 0x00441238 <----+ 0x0044123C <---+ ********** | ********** | ********** | var name: * a (+0) * | * a (+1) * | * a (+2) * | ********** | ********** | ********** | value: * 1 * | * 2 * | * 3 * | ********** | ********** | ********** | | | | | | | +-> 0x00442345 | +->0x00442349 | +->0x0044234D | | ************ | | ************ | | ************ | | * b (+0) * | | * b (+1) * | | * b (+2) * | | ************ | | ************ | | ************ | | *0x00441234* -+ | *0x00441238* --+ | *0x0044123C* --+ | ************ | ************ | ************ | | | | | | | 0x00443345 | 0x00443349 | 0x0044334D | ************ | ************ | ************ | * c (+0) * | * c (+1) * | * c (+2) * | ************ | ************ | ************ +-- *0x00442345* +-*0x00442349* +-*0x0044234D* ************ ************ ************

And each element of D points to each element of C, and so on. The end result being you're setting each element in each of the arrays (via some chain of pointers) back to the elements of a. And then in the second for loop you're printing the elements of a[] over and over again.

更多推荐

本文发布于:2023-07-23 00:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225225.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   程序   Output   closed   program

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!