在c阵列相关函数参数的区别 [英] difference of array related function arguments in C

查看:110
本文介绍了在c阵列相关函数参数的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了一个数组:

I have defined an array:

float array[3][4][5];

那么,有什么区别时

then, what is the difference when

array, array[0], array[0][0], &array[0][0][0]

作为函数的参数?

used as function argument?

推荐答案

学习最重要的是,在C,数组不作为的全部参数传递。取而代之的是,指针数组的第一元素传递

The important thing to learn is that, in C, arrays aren't passed as parameters in their entirety. Instead, the pointer to the first element of the array is passed.

所以,给出的定义 int数组[3] [4] [5]; ...

So, given the definition float array[3][4][5];...

阵列(作为参数)将类型浮动(*)[4] [5] ,指针浮标(解释的二维数组:我们不能通过该阵列,我们通过指针到它的第一个元素,这是一个4×5阵列,因此浮动(*)[4] [5] )。

array (as a parameter) will be of type float (*)[4][5], a pointer to a two-dimensional array of floats (explanation: we can't pass the array, we pass the pointer to its first element, which is a 4x5 array, hence float (*)[4][5]).

数组[0] (作为参数)将类型浮动(*)[5] ,一个指向花车(的一维数组的解释:数组[0] 终止的一个4x5阵列,我们无法通过阵列,我们通过指针第一它的元素,第一个元素是5个元素,因此浮动(*)的阵列[5] )。

array[0] (as a parameter) will be of type float (*)[5], a pointer to a one-dimensional array of floats (explanation: array[0] is a 4x5 array, we can't pass the array, we pass the pointer to the first element of it, the first element being an array of 5 elements, hence float (*)[5]).

阵列[0] [0] (作为参数)将键入浮法* ,指针为float(解释:阵列[0] [0] 5元素的数组,我们无法通过阵列,我们通过指针的第一个元素它的第一个元素是一个浮动的,因此浮法* )。

array[0][0] (as a parameter) will be of type float *, a pointer to a float (explanation: array[0][0] is an array of 5 elements, we can't pass the array, we pass the pointer to the first element of it, the first element being a float, hence float *).

&放大器;阵列[0] [0] [0] (作为参数)将类型浮法* ,指向一个浮动(解释:阵列[0] [0] [0] 浮动 ,我们通过它的指针,因此浮法* )。

&array[0][0][0] (as a parameter) will be of type float *, a pointer to a float (explanation: array[0][0][0] is a float, we pass a pointer to it, hence float *).

或许,一个更精细的例子:

Perhaps, a more elaborate example:

#include <stdio.h>

int x[3][5] =
{
  {  1,  2,  3,  4,  5 },
  {  6,  7,  8,  9, 10 },
  { 11, 12, 13, 14, 15 }
};

int (*pArr35)[3][5] = &x;
// &x is a pointer to an array of 3 arrays of 5 ints.

int (*pArr5a)[5] = x;
// x decays from an array of arrays of 5 ints to
// a pointer to an array of 5 ints,
// x is a pointer to an array of 5 ints.

int (*pArr5b)[5] = &x[0];
// &x[0] is a pointer to 0th element of x,
// x[0] is an array of 5 ints,
// &x[0] is a pointer to an array of 5 ints.

int *pInta = x[0];
// x[0] is 0th element of x,
// x[0] is an array of 5 ints,
// x[0] decays from an array of 5 ints to
// a pointer to an int.

int *pIntb = *x;
// x decays from an array of arrays of 5 ints to
// a pointer to an array of 5 ints,
// x is a pointer to an array of 5 ints,
// *x is an array of 5 ints,
// *x decays from an array of 5 ints to
// a pointer to an int.

int *pIntc = &x[0][0];
// x[0][0] is 0th element of x[0],
// where x[0] is an array of 5 ints,
// x[0][0] is an int,
// &x[0][0] is a pointer to an int.

int main(void)
{
  printf("&x=%p x=%p &x[0]=%p x[0]=%p *x=%p &x[0][0]=%p\n",
         pArr35, pArr5a, pArr5b, pInta, pIntb, pIntc);

  return 0;
}

输出( ideone ):

&x=0x804a040 x=0x804a040 &x[0]=0x804a040 x[0]=0x804a040 *x=0x804a040 &x[0][0]=0x804a040

这篇关于在c阵列相关函数参数的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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