Question about passing two-dimensional arrays as function parameters in C++

Reprinted from https://www.cnblogs.com/LeeZz/p/5084498.html

When doing the data structure maze experiment, we need to generate a two-dimensional array to store the maze data. In order to ensure the modularity of the program, the generated part is put into a function migongsc(). The approximate code is as follows:

Question about passing two-dimensional arrays as function parameters in C++

 

        The key to the problem is how to pass the two-dimensional array migong[8][9] declared in the main function to the custom function.

     After spending a night, I have sorted out three ways:

     Method 1: Simulate compiler addressing (this method comes from the CSDN blog, the original text:

http://blog.csdn.net/jatix/archive/2004/08/29/88150.aspx)。

      The general meaning is: when taking a two-dimensional array as a parameter, you must specify the size of all dimensions or omit the first dimension, but you cannot omit the size of the second dimension or higher, which is limited by the principle of the compiler. In fact, the compiler handles arrays like this:

      For the array int p[m][n], if you want to take the value of p[i][j] (i>=0 && i<m && 0<=j && j < n), the compiler addresses it like this , its address is:

 

         p + i*n + j;//Note n! !

         If the size of the second dimension or higher is omitted here, the compiler will not know how to address it correctly. According to this idea, we can simulate the compiler addressing method in the program, as follows:

Question about passing two-dimensional arrays as function parameters in C++

            Method 2: Simple method:

       Derived from method 1, as the name implies, the array name (ie the first address) of the declared two-dimensional array is directly passed to the custom function. But note that the dimension of the second dimension must be specified in the function definition!

Question about passing two-dimensional arrays as function parameters in C++

           Method 3: One-dimensionalization of two-dimensional arrays

        The way of instantiation of arrays in C++ is different from that in languages ​​such as VB. Multidimensional arrays can be said to be nested arrays, that is, in a two-dimensional array, each element is a one-dimensional array. Create a one-dimensional array to store the first address of each subarray (or element array), and pass the array to the custom function. Personally, I feel that in addition to reminding people that arrays can be nested. . . It's a very annoying way. . .

  Question about passing two-dimensional arrays as function parameters in C++

       Summary: Since arrays cannot be referenced in C++ (don't know why), the way to pass multidimensional arrays is all about pointers. Among the above three methods, the first method is the most extensible, and can achieve the wonderful effect of first defining the array and then allocating space (see the original text for details). The second method is simple and clear, in line with the habits of thinking. The third method. . . Uh. . . .

PS: If you have to use references, you can use two-dimensional vectors instead of arrays. The following is the code:

Question about passing two-dimensional arrays as function parameters in C++

Pay attention to the writing of the two-dimensional vector declaration: vector<vector<int> > migong(8);//Pay attention to the spaces! ! !

Corrections are welcome!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324840865&siteId=291194637
Recommended