数组到指针衰减并将多维数组传递给函数 [英] Array to pointer decay and passing multidimensional arrays to functions

查看:27
本文介绍了数组到指针衰减并将多维数组传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一个数组会衰减为一个指针,这样如果声明了一个指针

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

char things[8];

然后在其他地方使用 thingsthings 是指向数组中第一个元素的指针.

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];

那么 moreThings 不是 类型的指向 char 的指针,而是类型指向 char 的指针数组",因为衰减只发生一次.

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

moreThings 被传递给一个函数时(比如使用原型 void doThings(char thingsGoHere[8][8]) 堆栈实际发生了什么?

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

如果moreThings 不是指针类型,那么这真的还是一个传递引用吗?估计我一直以为moreThings还是代表多维数组的基地址.如果 doThings 接受输入 thingsGoHere 并将其传递给另一个函数会怎样?

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?

除非将数组输入指定为 const 否则该数组将始终是可修改的吗?

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.

推荐答案

你的理解有点错误:moreThings 也衰减到指向第一个元素的指针,但因为它是一个数组的数组字符,第一个元素是8 个字符的数组".所以衰减的指针是这种类型的:

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;

指针的当然和&moreThings[0][0]的值相同,即第一个元素的第一个元素的值,也与 &a 相同,但 type 在每种情况下都是不同的.

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.

这里有一个例子,如果char a[N][3]:

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:整个char数组的地址,就是一个char[N][3]

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

      &a[0],同a:第一个元素的地址,它本身就是一个char[3]

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

      &a[0][0]:第一个元素的第一个元素的地址,是一个char

      &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天全站免登陆