数组指针如何工作? [英] How pointer to array works?

查看:86
本文介绍了数组指针如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int s[4][2] = {
                  {1234, 56},
                  {1212, 33},
                  {1434, 80},
                  {1312, 78}
              };

int (*p)[1];
p = s[0];

printf("%d\n", *(*(p + 0))); // 1234
printf("%d\n", *(s[0] + 0)); // 1234
printf("%u\n", p);           // 1256433(address of s[0][0])
printf("%u\n", *p);          // 1256433(address of s[0][0])

任何人都可以解释为什么p = s[0]时为什么*(*(p + 0))打印1234并且*(s[0] + 0)也打印1234以及为什么p*p给出相同的结果吗?

Can anyone explain why doing *(*(p + 0)) prints 1234, and doing *(s[0] + 0) also prints 1234, when p = s[0] and also why does p and *p gives the same result?

感谢您的期待.

推荐答案

这是数组在C语言中的工作方式-数组不是第一类类型,因为除了声明它们并获取它们外,您不能对它们做任何其他事情他们的大小.在任何其他上下文中,当您使用带有(任何类型)数组类型的表达式时,该表达式将被静默转换为指向数组第一个元素的指针.通常将其称为分解"为指针的数组.

This is the way arrays work in C -- arrays are not first class types, in that you can't do anything with them other than declaring them and getting their size. In any other context, when you use an expression with type array (of anything) it is silently converted into a pointer to the array's first element. This is often referred to as an array "decaying" into a pointer.

因此,让我们一一查看一下您的陈述:

So lets look at your statements one by one:

p = s[0];

在这里,s具有数组类型(它是一个int[4][2]-一个二维int数组),因此它默默地转换成指向其第一个元素(一个int (*)[2]的指针)的指针,指向包含1234 ).然后,您可以使用[0]为该索引建立索引,这会将0 * sizeof(int [2])字节添加到指针,然后对其取消引用,从而为您提供int [2](2个整数的1D数组).由于这是一个数组,因此将其静默转换为指向其第一个元素(指向1234int *)的指针.请注意,这与索引之前的指针相同,只是指向类型的指针不同.

Here, s has array type (it's an int[4][2] -- a 2D int array), so its silently converted into a pointer to its first element (an int (*)[2], pointing at the word containing 1234). You then index this with [0] which adds 0 * sizeof(int [2]) bytes to the pointer, and then dereferences it, giving you an int [2] (1D array of 2 ints). Since this is an array, its silently converted into a pointer to its first element (an int * pointing at 1234). Note that this is the same pointer as before the index, just the pointed at type is different.

然后将此int *分配给声明为int (*)[1]p.由于C允许将任何指针分配给任何其他指针(即使所指向的类型不同),因此也可以使用,但是任何合理的编译器都会向您发出类型不匹配的警告.

You then assign this int * to p, which was declared as int (*)[1]. Since C allows assigning any pointer to any other pointer (even if the pointed at types are different), this works, but any reasonable compiler will give you a type mismatch warning.

p现在指向包含1234的单词(与您从s获得的指针指向的位置相同)

p now points at the word containing 1234 (the same place the pointer you get from s points at)

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

这首先将0*sizeof(int[1])添加到p并取消对其的引用,从而得到一个数组(int [1]),该数组立即衰减为其第一个元素的指针(int *仍指向同一位置).然后,该指针将被取消引用,从而给出要打印的int值1234.

This first adds 0*sizeof(int[1]) to p and dereferences it, giving an array (int[1]) that immediately decays to a pointer to its first element (an int * still pointing at the same place). THAT pointer is then dereferenced, giving the int value 1234 which is printed.

printf("%d\n", *(s[0]+0));

我们又有了s[0],它通过第一行描述中提到的多次衰减和取消引用过程变成了指向1234int *.我们向其中添加0*sizeof(int),然后取消引用,得到整数1234.

We have s[0] again which via the multiple decay and dereference process noted in the description of the first line, becomes an int * pointing at 1234. We add 0*sizeof(int) to it, and then dereference, giving the integer 1234.

printf("%u\n", p);

p是一个指针,因此只需打印该指针的地址即可.

p is a pointer, so the address of the pointer is simply printed.

printf("%u\n",*p)

取消引用

p,得到一个int [1](一维整数数组),该数组衰减为指向其第一个元素的指针.然后打印该指针.

p is dereferenced, giving an int [1] (1D integer array) which decays into a pointer to its first element. That pointer is then printed.

这篇关于数组指针如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆