1

我想通过将硬编码数组传递给它来测试一个采用运行时分配的多维数组的函数。

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

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

4

7 回答 7

6

不幸的是,这个多维数组主题让许多 C++ 程序员感到困惑。好吧,这是解决方案:

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

这就是函数声明的样子。另一种但完全等效的方法是

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

两者都在创建一个参数,该参数是指向 5 个整数数组的指针。然后,您可以将 5 个整数的数组数组传递给该函数:

generate_all_paths(arr, 5);

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

在评论中,您已经表明您已绑定到int**,因为您的内部和外部维度都必须具有运行时值。不能再使用多维数组了。然后,出于测试目的,您可以创建一个指针数组,如下所示:

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

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

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

store 是你的int[5][5]数组。由于store[n]访问了该二维数组的第n个子数组,其元素类型为int,其指针转换类型为int*,将再次兼容。

于 2009-02-11T06:48:19.617 回答
3

你可以写:

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
于 2009-05-23T20:11:31.630 回答
1

还有,为什么数组是保留字?

它不是。您可能正在使用 Visual Studio,因为它在 C++/CLI 中用作本机托管类型,因此它显示为关键字。但是,这与 C++ 无关,Visual Studio 在这方面具有误导性。

至于您的问题:您可以简单地传递一个指向指针的指针,然后直接传递您的嵌套数组(前提是您正在使用动态分配的数组):

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;
}

为了利用这一点,您的调用代码也需要更改以使用这些数据结构而不是chars 上的普通 C 数组。

于 2009-05-23T20:10:05.767 回答
1

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

于 2009-05-23T20:12:45.527 回答
0

arr是指向您拥有的多维数组的指针,实际上是指向int. 现在,由于您的函数接受指向指针的int指针,因此您需要获取arrusing:的地址&arr并将其传递给函数,以便您拥有以下代码:

强制数组:传递&arr给函数。要在 func 中引用数组:*maze[x][y]

于 2009-02-11T06:55:19.740 回答
0

最好的方法是使用指针,但 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;
}

///////////////////////////////////

希望这就是你的意思。

于 2009-05-24T00:49:16.123 回答
0

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

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

然后你可以像这样引用元素x,y(x沿宽度,y沿高度)

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

This may be however a bit slower than the solution above (measured on GCC 3.4.5), so if you are not interested in continuous memory (for example you always access the elements with [][], never by adding integer to a pointer and dereferencing it), then you should use the array af arrays. However, if you are interested in having the continuous memory, e.g. to pass it as initializer to an std::string object or to send it as a whole through a network, you should use the second one.

于 2009-05-24T01:19:33.973 回答