数组指针腐烂,并通过多维阵列功能 [英] Array to pointer decay and passing multidimensional arrays to functions

查看:152
本文介绍了数组指针腐烂,并通过多维阵列功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道数组衰减到一个指针,使得如果一个所声明的

 字符的事情; [8]

再后来就用的事情其他地方,的事情是一个指向数组的第一个元素。

此外,从我的理解,如果一个声明

 字符moreThings [8] [8];

然后 moreThings 终止的的类型字符指针,但类型的指针为char数组,因为衰变只发生一次。

moreThings 传递给函数(原型说无效doThings(CHAR thingsGoHere [8] [8])什么是真正发生的事情与堆栈?

如果 moreThings 不是指针类型的,那么这是真的还是一个传递引用?我想我一直以为 moreThings 还是重新presented的多维数组的基址。如果 doThings 输入了 thingsGoHere 和它本身传递到另一个函数?

是规则pretty多,除非你指定一个数组输入为常量则数组会一直修改?

我知道类型检查的东西只发生在编译​​的时候,但我仍然感到困惑的是什么引用技术上算作合格(即是它,只有当指针类型的参数传递,或将指针数组是一个传递引用呢?)

对不起,是一个小遍这个问题的地方,但因为我的理解这个困难的,很难说出一个precise询问。


解决方案

您得到了它稍有不妥: moreThings 也衰减到一个指向第一个元素,但由于它是字符数组的数组,第一元件是一个8字符的数组。所以腐朽的指针是这种类型的:

 字符(* P)[8] = moreThings;

的指针当然是一样的值&放大器; moreThings [0] [0] ,即在第一个元素的第一个元素,也是同样的&放大器;一个,而键入的是一个不同的每种情况。

下面是一个例子,如果字符一个[N] [3]

  + =========================== + ========= ================== + ====
| + -------- + -------- + ------- + | + -------- + -------- + - ----- + |
||一个[0,0] |一个[0,1] |一个[0,2] |||一个[1,0] |一个[1,1] |一个[1,2] || ...
| + -------- + -------- + ------- -------- +++ + -------- + - ----- ++ ...
|一个[0] |一个[1] |
+ =========================== + ===================== ====== + ====
                                    一个
^^^
|| + - &放大器;一个[0,0]
| + - - - - &放大器;一个[0]
------- +功放&; A


  • &放大器;一个:字符数组的整个数组,这是一个的char [N]的地址[3]


  • &放大器;一个[0] ,同 A :第一个元素,地址其中本身就是一个的char [3]


  • &安培; A [0] [0] :第一个元素的第一个元素,它是字符<地址/ code>


此表明不同的对象可能有相同的地址,但是,如果两个对象具有相同的地址的的相同的类型,则它们是相同的对象。

I know that an array decays to a pointer, such that if one declared

char things[8];

and then later on used things somewhere else, things is a pointer to the first element in the array.

Also, from my understanding, if one declares

char moreThings[8][8];

then moreThings is not of type pointer to char but of type "array of pointers to char," because the decay only occurs once.

When moreThings is passed to a function (say with prototype void doThings(char thingsGoHere[8][8]) what is actually going on with the stack?

If moreThings is not of pointer type, then is this really still a pass-by-reference? I guess I always thought that moreThings still represented the base address of the multidimensional array. What if doThings took input thingsGoHere and itself passed it to another function?

Is the rule pretty much that unless one specifies an array input as const then the array will always be modifiable?

I know that the type checking stuff only happens at compile time, but I'm still confused about what technically counts as a pass by reference (i.e. is it only when arguments of type pointer are passed, or would array of pointers be a pass-by-reference as well?)

Sorry to be a little all over the place with this question, but because of my difficulty in understanding this it is hard to articulate a precise inquiry.

解决方案

You got it slightly wrong: moreThings also decays to a pointer to the first element, but since it is an array of an array of chars, the first element is an "array of 8 chars". So the decayed pointer is of this type:

char (*p)[8] = moreThings;

The value of the pointer is of course the same as the value of &moreThings[0][0], i.e. of the first element of the first element, and also the same of &a, but the type is a different one in each case.

Here's an example if char a[N][3]:

+===========================+===========================+====
|+--------+--------+-------+|+--------+--------+-------+|
|| a[0,0] | a[0,1] | a[0,2]||| a[1,0] | a[1,1] | a[1,2]|| ...
|+--------+--------+-------+++--------+--------+-------++ ...
|            a[0]           |            a[1]           |
+===========================+===========================+====
                                    a
^^^
||+-- &a[0,0]
|+-----&a[0]
+-------&a

  • &a: address of the entire array of arrays of chars, which is a char[N][3]

  • &a[0], same as a: address of the first element, which is itself a char[3]

  • &a[0][0]: address of the first element of the first element, which is a char

This demonstrates that different objects may have the same address, but if two objects have the same address and the same type, then they are the same object.

这篇关于数组指针腐烂,并通过多维阵列功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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