什么是数组[行数]的[COLS],差异**阵列 [英] What is the difference between array[ROWS][COLS] and **array

查看:124
本文介绍了什么是数组[行数]的[COLS],差异**阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不是这些函数原型相同呢?

 无效print_matrix(字符*名称,诠释SX,SY INT,INT米[SX] [SY])无效print_matrix(字符*名称,INT SX,SY INT,INT ** M)


解决方案

尽管这两个函数的参数可以的消耗的以同样的方式,即通过 M [] [J] ,他们是完全不同的:


  • INT米[M] [N] M <$ C数组的数组$ C> N 整数。


  • INT ** M 是一个指针的指针为int。


您不能传递数组作为函数的参数,所以一个 K的阵列 T类型的元素衰减到一个POIN&害羞;之三至 - T ,指向数组的第一个元素。因此,它是允许的,等同于写第一种形式为 INT M [] [N] 函数中的参数,因为该值 M 丢失。但是,值 N 是的的丢失;它是类型的一部分​​!

因此​​,以下是容许的/错误的第一种形式:

 无效F(INT改编[M] [N]);int类型的[M] [N];
INT B〔2 * M] [N];
INT C [M] [N + 1];F(一); // 好
F(二); // 好;最慢的程度遗忘在衰变
// F(C); //错误! C是不是{阵列ň的int}的数组。

有关的第二种形式,意思是相当不同的

 无效克(INT ** P);int类型的;
为int * PA =安培; A;克(&安培; PA); //很好,指向啪INT ARR [M] [N];//克(ARR); //错误,是没有意义的

这位前pression 改编指定的指针的 N 数组的数组的第一个元素整数,即其类型为 INT(*)[N] 。解引用它给人的为N的数组整数和的的指针为整数。

有没有办法除权pression 改编转换成一个指针的指针:如果你说的,

  INT **傻瓜=(INT **)改编;

然后 *傻瓜将指向第一个数组的第一个元素(改编[0] ),并的不是一个 INT 指针的。所以你不能提领价值进一步,因为该值的不是指针

的唯一的正确的方式来传递的二维阵列作为双指针是构造一个中间辅助阵列

 为int *帮手[M]。 //指针数组用于(为size_t我= 0;!我= M ++ I)
{
    帮手[I] =改编[I] //隐衰减
}克(助手); //或克(&放大器;辅助[0])

Why aren't these function prototypes equivalent?

void print_matrix(char *name, int SX, int SY, int m[SX][SY])

void print_matrix(char *name, int SX, int SY, int **m)

解决方案

Even though the two function arguments can be consumed in the same way, namely via m[i][j], they're quite different:

  • int m[M][N] is an array of M arrays of N ints.

  • int **m is a pointer to a pointer to an int.

You cannot pass arrays as function arguments, so an "array of K elements of type T" decays to a "poin­ter-to-T", pointing to the first element of the array. Thus it is permissible and equivalent to write the first form as int m[][N] in a function argument, since the value M is lost. However, the value N is not lost; it is part of the type!

So the following are admissible/erroneous for the first form:

void f(int arr[M][N]);

int a[M][N];
int b[2*M][N];
int c[M][N + 1];

f(a);   // OK
f(b);   // OK; slowest extent is forgotten in the decay
//f(c); // Error! 'c' is not an array of {array of N ints}.

For the second form, the meaning is rather different:

void g(int **p);

int a;
int * pa = &a;

g(&pa);          // fine, pointer to 'pa'

int arr[M][N];

// g(arr);  // Error, makes no sense

The expression arr designates the pointer to the first element of an array of arrays of N integers, i.e. its type is int (*)[N]. Dereferencing it gives an array of N integers, and not a pointer to an integer.

There is no way to convert the expression arr into a pointer to a pointer: If you said,

int ** fool = (int**)arr;

then *fool would point to the first element of the first array (arr[0]), and not to an int pointer. So you cannot dereference the value further, because the value is not a pointer.

The only correct way to pass a two-dimensional array as a double pointer is to construct an intermediate helper array:

int * helper[M];   // array of pointers

for (size_t i = 0; i != M; ++i)
{
    helper[i] = arr[i]; // implicit decay
}

g(helper);  // or "g(&helper[0])"

这篇关于什么是数组[行数]的[COLS],差异**阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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