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

查看:25
本文介绍了如何将多维数组传递给 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?

推荐答案

此代码在 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.)

所以,再一次,不要做出不正确的断言.这段代码在 C 中不起作用.为了将内置的 2D 数组转换为 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

在 c++ 中将多维数组转换为指针

(请参阅已接受的答案.问题完全相同.)

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

代码似乎可以在 C 中工作,因为打印代码中的另一个错误正在伪装数组传递中错误的影响.为了正确访问 int ** 伪数组的元素,您必须使用表达式 *(*(arr + i) + j),或者更好的是普通 arr[i][j] (这是同一件事).您错过了额外的 * ,这使它打印出与数组内容完全无关的内容.同样,将 main 中的数组初始化为其他内容,以查看您在 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 语句,你的代码很可能会因为我最初描述的数组传递错误而崩溃.

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