如何多维数组传递给C和C函数++ [英] How to pass a multidimensional array to a function in C and C++

查看:107
本文介绍了如何多维数组传递给C和C函数++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
void print(int *arr[], int s1, int s2) {
    int i, j;
    for(i = 0; i<s1; i++)
        for(j = 0; j<s2; j++)
            printf("%d, ", *((arr+i)+j));
}

int main() {
    int a[4][4] = {{0}};
    print(a,4,4);
}

这工作在C,但不是在C ++中。

This works in C, but not in C++.

错误:

cannot convert `int (*)[4]' to `int**' for argument `1' to 
`void print(int**, int, int)'

为什么它不是在C ++工作?需要什么样的变化进行?

Why does it not work in C++? What change is needed to be made?

推荐答案

这code会的的用C或C ++的工作。类型的数组 INT [4] [4] 是无法转换为类型的指针 INT ** (其中就是为int * ARR [] 在参数声明中表示)。如果你用C设法的编译的,它很简单,因为你可能忽略了基本相同的格式,你从C ++编译器得到的错误消息的C编译器警告。 (有时C编译器问题的警告的什么本质上是一个的错误的。)

This code will not work in either C or C++. An array of type int[4][4] is not convertible to a pointer of type int ** (which is what int *arr[] stands for in parameter declaration). If you managed to compile it in C, it is simply because you probably ignored a C compiler warning of basically the same format as the error message you got from C++ compiler. (Sometimes C compilers issue warnings for what is essentially an error.)

所以,再次,不要让那些不正确的说法。这code没有在C.工作。为了转换内置的二维数组成 INT ** 指针时,可以使用的技术像这样的

So, again, don't make assertions that are not true. This code does not work in C. In order to convert a built-in 2D array into a int ** pointer you can use a technique like this one

<一个href=\"http://stackoverflow.com/questions/1584100/converting-multidimensional-arrays-to-pointers-in-c\">Converting多维数组在C ++

(参见接受的答案,问题是完全一样的。)

(See the accepted answer. The problem is exactly the same.)

编辑:的code的出现的在C工作,因为在印刷code另一个错误是伪装的bug影响数组传递。为了正确访问为int的元素** 伪数组,你必须使用前pression *(*(ARR +我)+ J),或更好的一个普通的改编[I] [J] (这是同样的事情)。你错过了额外的 * 这使得它打印的东西,有绝对无关,与你的数组的内容。再次,初始化数组中的别的东西一看就知道你是在C印花效果绝对无关的数组您预期的内容。

The code appears to work in C because another bug in the printing code is masquerading the effects of the bug in array passing. In order to properly access an element of an int ** pseudo-array, you have to use expression *(*(arr + i) + j), or better a plain arr[i][j] (which is the same thing). You missed the extra * which made it print something that has absolutely nothing to do with the content of your array. Again, initialize your array in main to something else to see that the results you are printing in C have absolutely nothing to do with the your intended content of the array.

如果你改变了的printf 语句如上图所示,你的code会因为我最初描述的阵列传递的bug最有可能崩溃。

If you change the printf statement as shown above, your code will most likely crash because of the array-passing bug I described initially.

再来一次:你不能将一个 INT [4] [4] 数组作为 INT ** 伪数组。这是C ++的是告诉你错误消息。而且,我敢肯定,这就是你的C编译器告诉你,但你可能忽略了它,因为它是只是一个警告。

One more time: you cannot pass a int[4][4] array as an int ** pseudo-array. This is what the C++ is telling you in the error message. And, I'm sure, this is what your C compiler told you, but you probably ignored it, since it was "just a warning".

这篇关于如何多维数组传递给C和C函数++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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