代码之家  ›  专栏  ›  技术社区  ›  Mike Douglas

将int[5][5]类型的变量传递给需要int的函数**

  •  1
  • Mike Douglas  · 技术社区  · 17 年前

    void generate_all_paths(int** maze, int size) 该数组定义为 int arr[5][5] = {REMOVED} .

    我不确定如何正确地强制函数的数组(或者如果不可能)。

    7 回复  |  直到 14 年前
        1
  •  6
  •   Johannes Schaub - litb    17 年前

    这个多维数组话题不幸地混淆了这么多C++程序员。好吧,解决方案如下:

    void generate_all_paths(int (*maze)[5], int size);
    

    这就是函数声明的样子。另一种选择,但完全等效

    void generate_all_paths(int maze[][5], int size);
    

    generate_all_paths(arr, 5);
    

    因为数组的第一个元素是5个整数的数组,所以当传递给该函数时,它将自动(隐式)转换为指向第一个元素的指针。

    在注释中,您已显示您已绑定到 int**

    int store[5 * 5] = { ..... };
    int *arr[5] = { store, store + 5, store + 10, store + 15, store + 20 };
    

    然后,实际上,你可以让你的函数接受一个 int** . 因为数组的第一个元素是 int* int** 自动地另一种方法是将数据保留在二维数组中,但只是创建一个由指向该数组的指针构成的“视图”:

    int *arr[5] = { store[0], store[1], store[2], store[3], store[4] };
    

    你的商店在哪里 int[5][5] store[n] 访问该二维数组的第n个子数组,其元素类型为 int ,它的指针转换类型为

        2
  •  3
  •   Daniel Earwicker    17 年前

    你可以写:

    void display(char **a)
    

    然后使用 a[i][j]

    宣言 char ** 表示“指向整数的指针”。要将其分解为以下步骤:

    char *b = a[i];
    

    i '数组数组中的第个数组。

    char c = b[j];
    

    这样你就有了 j b .

    下一个问题是如何分配这样一个数组。

    char **arrayOfArrays = new char *[10];
    
    for (int n = 0; n < 10; n++)
        arrayOfArrays[n] = new char[20];
    

    它分配一个由10个数组组成的数组,每个“子”数组有20个字符。

    在C/C++中,数组访问语法只是一种检索距离指针一定距离的值的方法。

    char *p = "Hello";
    
    char *pl = p + 2;   // get pointer to middle 'l'
    char l = *pl;       // fetch
    
    char o = p[4];      // use array syntax instead
    
        3
  •  1
  •   cagribal    17 年前

    void display(char ** array) 应该有用。我也不认为它是标准C/C++中的保留字。

        4
  •  1
  •   Konrad Rudolph    17 年前

    事实并非如此。您可能正在使用Visual Studio,因为它在C++/CLI中作为本机托管类型使用,所以它显示为关键字。然而,这与C++无关,Visual Studio在这方面是误导的。

    至于您的问题:您可以简单地传递指向char的指针的指针,然后直接传递嵌套数组( 假设您使用的是动态分配的阵列 ):

    void display(char** array) …
    

    也就是说,您的函数假定一个固定的、已知的数组长度和一些其他细节。最好使用嵌套的 std::vector 或 std::string 很 更容易的。

    void display(std::vector<std::string> const& array) {
        for (size_t i = 0; i < array.length(); ++i)
            cout << array[i] << endl;
    }
    

    char s

        5
  •  0
  •   poliklosio    17 年前

    窃听器的回答遗漏了一个重要事实。他提议的是一系列的阵列。对于第一种情况,这会浪费指针数组的内存(“char**arrayOfArrays=new char*[10]”是该数组的创建点)。对于第二种情况,字符数组可能不是一个连续的内存块,这通常是一个问题。 C++中唯一的解决方法是创建一维数组,并在需要时计算索引。

    char *b = new char[width*height];
    

    char c=b[width*y+x];
    

    但是,这可能比上面的解决方案慢一点(在GCC 3.4.5中测量),因此如果您对连续内存不感兴趣(例如,您总是使用[]访问元素,而不是通过向指针添加整数并取消引用它),那么您应该使用数组af数组。但是,如果您对拥有连续内存感兴趣,例如将其作为初始值传递给std::string对象或通过网络将其作为一个整体发送,则应使用第二个。

        6
  •  0
  •   Jason Plank Maksim Kondratyuk    14 年前

    最好是使用指针,但是Borland C++允许传递数组作为函数的参数。查看此代码(包括:iostream和conio):

    ////////////////////////////////////////////
    
    void ReceivedArray(char x[5]){
    
    for (int i=0; i<5; i++ )
    cout << x[i];
    
    }
    
    void main(){
    
    char *x = new char[5];
    
    for (int i=0; i<5; i++ )
    x[i]='o';
    
    ReceivedArray(x);
    getchar();
    }
    
    
    ///////////////////////////////////////////////////////////////
    
    
    For passing 2D arrays (oops! some lines in spanish, sorry!):
    
    (includes: iostream, stdlb, stdio and math)
    
    
    /////////////////////////////////////////////////
    
    using namespace std;
    
    void ver(int x[][20]){
    for(int i=0; i<15; i++)  {
      for(int j=0; j<20; j++) {
       cout<< x[i][j] <<" ";       }
       cout << "\n";        }
    
    }
    
    void cambiar0(int x[][20]){ int n[255];
       for (int i=255; i>=0; i--)
        n[255-i]=i;
    
    
    for(int i=0; i<15; i++)
      for(int j=0; j<20; j++)
          for(int k=0; k<255; k++)
           if(x[i][j]==n[k])  {
              x[i][j]=k; break;  }  
    }
    
    int main(int argc, char* argv[]){
    int x[15][20]; char a; 
    
    for(int i=0; i<15; i++)
      for(int j=0; j<20; j++)
       x[i][j]=rand()%255;
    
      cout << "¿desea ver la matriz? s/n ";
     cin >> a;
     if(a=='s') ver(x);
    
     cambiar0(x);
    
     cout << "\n\n";
     cout << "¿desea ver la matriz? s/n ";
     cin >> a;
     if(a=='s') ver(x);
    
    system("PAUSE"); return 0;
    }
    
    ///////////////////////////////////
    

        7
  •  0
  •   Jason Plank Maksim Kondratyuk    14 年前

    arr int . 现在,由于您的函数接受指向 int 啊 使用: &arr 并将其传递给函数,以便获得以下代码:

    强制数组:Pass & 对函数进行修改。 要引用func中的数组,请执行以下操作: *maze[x][y]