传递数组和矩阵,以功能作为指针,指针的指针用C [英] Passing arrays and matrices to functions as pointers and pointers to pointers in C

查看:110
本文介绍了传递数组和矩阵,以功能作为指针,指针的指针用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下code:

void
foo( int* array ) 
{
    // ...
}

void
bar( int** matrix ) 
{
    // ...
}

int
main( void ) {
    int array[ 10 ];
    int matrix[ 10 ][ 10 ];

    foo( array );
    bar( matrix );

    return 0;
}

我不明白为什么我得到这样的警告:

I don't understand why I get this warning:

警告:从兼容的指针类型过客酒吧的参数1

warning: passing argument 1 of ‘bar’ from incompatible pointer type

虽然'富'呼叫似乎是确定。

Although 'foo' call seems to be ok.

感谢:)

推荐答案

那么,它肯定不是很好的C社区理解为可以一眼过来,让待观察。神奇的是,所有以下完全,100%,相当于

Well, it's certainly not well understood by the C community as can be seen by glancing over SO. The magic is, all of the following are totally, 100%, equivalent:

void foo(int (*array)[10]);
void foo(int array[][10]);
void foo(int array[10][10]);
void foo(int array[42][10]);

这是的非常的重要画一个指针和数组的区别。的数组是不是指针的。阵列可以被转换为一个指向它的第一个元素。如果你有一个指针,你有这样的:

It is very important to draw the distinction of a pointer and an array. An array is not a pointer. An array can be converted to a pointer to its first element. If you have a pointer you have this:

--------
| ptr  |  -------> data
--------

不过,如果你有一个数组,你有这样的:

However, if you have an array, you have this:

---------------------------
| c1 | c2 | c3 | ... | cn |
---------------------------

使用指针时,数据是在一个整体的其他行星,但由指针链接。阵列具有数据本身。现在,多维数组只是数组的数组。的阵列被嵌套在一个父阵列。所以,你的数组的sizeof是:

With the pointer, the data is at a whole other planet, but linked to by the pointer. An array has the data itself. Now, a multi-dimensional array is just an array of arrays. The arrays are nested into a parent array. So, the sizeof of your array is:

(sizeof(int) * 10) * 10

这是因为你有10个阵列,所有这些都是10的整数阵列。现在,如果你想通过该数组,它被转换。但是,为了什么呢?一个指向它的第一个元素。元素类型的的指针,而是一个数组。因此,你传递一个指向10整数的数组:

That is because you have 10 arrays, all of which are arrays of 10 integers. Now, if you want to pass that array, it is converted. But to what? A pointer to its first element. The element type is not a pointer, but an array. As a consequence, you pass a pointer to an array of 10 int:

int (*)[10] // a pointer to an int[10]

这既不是一个为int * 的数组,也不是 INT ** 。你可能会问,为什么阵列不是作为一个通过INT ** 。这是因为编译器必须知道行长度。如果你做一个数组[1] [0] ,编译器将针对一个地方的sizeof(int)的* 10 从开始的2维阵列的字节分开。据德codeS,在指针到数组类型的信息。

It is neither a array of int*, nor a int**. You may ask why the array is not passed as an int**. It's because the compiler has to know the row-length. If you do an array[1][0], the compiler will address a place sizeof(int) * 10 bytes apart from the begin of the 2 dimensional array. It decodes that information in the pointer-to-array type.

所以,你有上面的完全等价的函数原型之一之间进行选择。当然,最后一个就是混乱。编译器只是默默忽略写在最外部尺寸,如果一个参数被声明为一个数组的任何数字。所以,我也不会用第二个最后一个版本。最好是用第一或第二个版本。重要的是要记住的是, C有没有(真正)数组参数!参数将在端部的指针(指针阵列中的这种情况下)。

So, you have to chose among one of the above fully equivalent function prototypes. Naturally, the last one is just confusing. The compiler just silently ignores any number written in the most outer dimension if a parameter is declared to be an array. So i would also not use the second last version. Best is to use the first or second version. What is important to remember is that C has no (real) array parameters! The parameter will be a pointer in the end (pointer to array in this case).

请注意上述多维情况下如何是类似于简并的,下面的一维情况。以下所有4个版本是完全等效的:

Note how the multi-dimensional case of above is similar to the degenerate, one dimensional case below. All of the following 4 versions are fully equivalent:

void foo(int *array);
void foo(int array[]);
void foo(int array[10]);
void foo(int array[42]);

这篇关于传递数组和矩阵,以功能作为指针,指针的指针用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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