如何处理数组指针?
该程序正确打印所有数组输出。但这个程序到底是如何运作的呢?为什么这里需要s[i]
的地址?
#include <stdio.h>
int main(){
int s[4][2] = {{1234,1},{1233,2},{1232,3},{1331,4}};
int (*p)[2];
int i,j,*pint;
for(i=0;i<4;++i){
p = &s[i];
pint = (int*)p;
printf("\n");
for(j=0;j<=1;++j)
printf("%d ",*(pint+j));
}
return 0;
}
This program prints all arrays outputs correctly. But how does this program work exactly? Why do we need address of s[i]
here?
#include <stdio.h>
int main(){
int s[4][2] = {{1234,1},{1233,2},{1232,3},{1331,4}};
int (*p)[2];
int i,j,*pint;
for(i=0;i<4;++i){
p = &s[i];
pint = (int*)p;
printf("\n");
for(j=0;j<=1;++j)
printf("%d ",*(pint+j));
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
int (*p)[2]
是一个指向int [2]
类型的指针,它本身本质上是一个int *
类型,但具有对数组边界进行一些额外的类型检查。因此,在
p = &s[i];
中,您将p
设置为指向内存中数组s[i 所在区域的指针的地址]
是。让
p
也成为一个数组(即,“本质上”是一个指向带有附加机制的内存区域的指针)要容易得多,然后直接使用它来指向数组内存区域(<代码>p = s[i])。然而,在这种情况下,这正是pint
(作为真正的指针)正在做的事情,所以我们可以完全删除p
。因此:
请参阅数组和指针或谷歌搜索“C 数组和指针”还有 C 多维中的指针地址数组。
请注意,我假设您这样做只是为了玩弄并了解指针和数组如何交互,因此我不会评论在每种情况下使用指针算术或数组概念等是否理想。
另请注意,我说数组“本质上是一个指针 [...]”,但是,这并不完全正确,它在很多情况下只是充当指针,并且在大多数情况下,这是一种合理的方式想想事情是如何运作的。实际上,数组是以特殊方式处理的。请参阅 数组名称等同于指针吗? 和 数组。
int (*p)[2]
is a pointer to anint [2]
type, which itself is essentially aint *
type but with some additional type checking on the array bounds.So in
p = &s[i];
you are settingp
to be the address of the pointer to the area in memory where the arrays[i]
is.It's much much easier to just make
p
also an array (i.e., "essentially" a pointer to an area in memory with additional machinery), and then use that directly to point at the array memory area (p = s[i]
). However, in that case, that's exactly whatpint
(as a true pointer instead) is doing, so we can removep
entirely.So:
See Arrays and pointers or google for "C arrays and pointers" and also Pointer address in a C multidimensional array.
Note, I assume you are just doing this to play around and understand how pointers and arrays interact, so I make no comment on whether it is ideal to use pointers arithmetic or array notion and so on in each case.
Note also, that I say an array is "essentially a pointer [...]", however, this is not strictly true, it just acts like a pointer in a lot of cases and for the most part this is a reasonable way to think of how things are working. In reality, arrays are treated in a special way. See Is array name equivalent to pointer? and Arrays.
这里 p 被定义为一个数组,它可以存储两个整数值,
在外部 for 循环中我们使用 p 数组来存储 4 {1 维} 。一次 1 个。
也就是说,在第一次迭代中,它将是
下一次迭代
,依此类推。这就是为什么我们需要 &S[i]
内循环
只需迭代一维数组即可打印元素
here p is defined as an array which can store two integer values
in the outer for loop we use p array to store the 4 {1 dimensional} . 1 at a time .
that is in first iteration it will be
the next iteration will be
and so on. that is why we need &S[i]
the inner loop
just iterate throug the 1 dimensional array to print the element
啊。我不确定这段代码除了如何使易于理解的代码难以理解之外还应该说明什么。
s
是int
2 元素数组的 4 元素数组。也就是说,对于 0..3 中的i
,s[i]
的类型是int [2]
(在大多数情况下会衰减)为int *
),&s[i]
的类型为int (*)[2]
。p
是一个指向int
2 元素数组的指针。每次循环时,p
都会被分配s[i]
处的 2 元素数组的地址。最后,pint是一个指向int的简单指针,每次循环都被设置为指向p指向的数组中的第一个元素代码>.
*(pint + j)
是pint[j]
的长写法。请注意,
p
和pint
都是完全多余的;这基本上是一种冗长的写作方式Ugh. I'm not sure what this code is supposed to illustrate other than how to make easy-to-understand code hard to understand.
s
is a 4-element array of 2-element arrays ofint
. That is, fori
in 0..3, the type ofs[i]
isint [2]
(which in most contexts decays toint *
), and the type of&s[i]
isint (*)[2]
.p
is a pointer to a 2-element array ofint
. Each time through the loop,p
is assigned the address of the 2-element array ats[i]
.Finally,
pint
is a simple pointer toint
, and each time through the loop is set to point to the first element in the array pointed to byp
.*(pint + j)
is the long way of writingpint[j]
.Note that both
p
andpint
are entirely superfluous; this is basically a long-winded way of writing