将动态 3D 数组发送到 C++ 中的函数

send a dynamice 3D array to a function in c++

本文关键字:C++ 函数 动态 3D 数组      更新时间:2023-10-16

我是C++编程的早期。我想将 3D 数组发送到函数。

const int MaxNumberOfLegs=function1();
double D_and_L_Of_Legs[4][2][MaxNumberOfLegs] = {        { {1, 2, 3.4, 4}   , {1, 2, 3, 4} },
                                 { {1, 2, 3, 4.5}   , {1, 2, 3} }, 
                                 { {1.8, 2, 3, 4}   , {1, 2, 3, 4} }, 
                                 { {}   , {} }  
                        };
function2(D_and_L_Of_Legs);

和函数 2 相同:

void ProcessImage(double D_and_L_Of_Legs[4][2][MaxNumberOfLegs]){
}

在我的代码中,MaxNumberOfLegs变量使用 function1 而不是 const 变量进行计算。 在 C++ 中,当我们想要发送数组时,我们必须编写数组的 dimention。现在我的问题是,当第三维不是常量时,我如何发送 3d 数组?

这个函数可以:

void ProcessImage(double D_and_L_Of_Legs[][][], int MaxNumberOfLegs){
}

当您想打电话时,请执行以下操作:

const int MaxNumberOfLegs=function1();
   double D_and_L_Of_Legs[4][2][MaxNumberOfLegs] = {
                                     { {1, 2, 3.4, 4} ,{1, 2, 3, 4} },
                                     { {1, 2, 3, 4.5}   , {1, 2, 3} }, 
                                     { {1.8, 2, 3, 4}   , {1, 2, 3, 4} }, 
                                     { {}   , {} }  
                            };
    function2(D_and_L_Of_Legs,MaxNumberOfLegs);

替代方法:

尝试将函数MaxNumberOfLegs声明为全局变量并保持所有代码相同。 即int MaxNumberOfLegs=function1();将其放在代码的顶部。 并在函数内部制作MaxNumberOfLegs=function1(); .