本文介绍了c ++动态数组初始化与声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的功能:

void findScarf1(bool ** matrix, int m, int n, int radius, int connectivity);

和在 main 函数中创建2d动态数组以传递此函数

and in main function I create 2d dynamic array to pass in this function

    bool matrix[6][7] = {
    {0, 0, 1, 1, 1, 0, 0},
    {0, 0, 1, 1, 1, 0, 0},
    {0, 0, 1, 1, 1, 0, 0},
    {0, 0, 1, 1, 1, 0, 0},
    {0, 0, 1, 1, 1, 0, 0},
    {0, 0, 1, 1, 1, 0, 0}
};

问题是:

findScarf1(matrix, 6, 7, 3, 4);

导致
错误C2664:'findScarf1':无法将参数1从' bool **'

如何初始化数组紧凑(与声明同时)?

ps对不起,如果它是重复的问题,但我花了1.5小时计算出来

p.s. sorry if it's duplicate question but I've spent 1.5 hours figuring it out

推荐答案

数组。所以它不能转换成指针的指针。但它可以转换成指向数组的指针。

Technically, a 2D array is an array of 1D arrays. So it cannot convert into pointer to pointer. It can convert into pointer to array, though.

所以这应该工作:

void findScarf1(bool (*matrix)[7], int m, int n, int radius, int connectivity);

这里 bool(* matrix)[7] 声明一个指向7 bool 数组的指针。

Here bool (*matrix)[7] declares a pointer to array of 7 bool.

希望有帮助。

这篇关于c ++动态数组初始化与声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 17:12
查看更多