How to do a function interface that can receive any two-dimensional array in C + +, and can refer to the element __ function in the form of the following object in the function

Source: Internet
Author: User

Some time ago, in the use of gtk+2.0 to do a greedy snake game, there is such an idea, the game of each of the obstacles to increase, and the map to become larger, that is, the program in the two-dimensional array to change. So there is a need to do a function interface that can receive arbitrary two-dimensional arrays, and in a function to refer to array elements in the form of arrays, such as a[3][2] (which emphasizes referencing elements in this way).
However, to pass a two-dimensional array in C + +, then the formal parameter of the function must specify the dimension of the second dimension, such as void fun (int a[][3]), or void fun (int (*a) [3]), which is the number of offset factors that must tell the compiler block pointer, such as (A + 1) is actually (a+1* offset factor), I just thought that it would be possible to assign the first address of a two-dimensional array to a pointer to a one-dimensional array, and then control the offsets yourself, which is true, but the reference element cannot be referenced in the following form. If this is the case, the algorithm written earlier or the function for a particular two-dimensional array is converted into functions that can handle any two-dimensional array, there are too many things to change, in order to be lazy, super hope can still use like a[2][3] such as the subscript method to refer to elements, so just a little change the interface on the line.
After a few days of thinking, I finally thought of a simple and convenient method, although no big deal, can only be a small skill, share to everyone. (estimate this kind of trick, everybody wants to be laughed at, the loss I still have the nerve to take out)
I thought of an array of pointers such as int *a[2]; It cannot be distinguished from a two-dimensional array when it is referenced, like a[1][2], and you cannot infer whether a is a two-dimensional array name or an array of pointers. So I thought about converting a two-dimensional array into an array of pointers.
/* Achieving a common array transform as a point array */
int * * Deal_array (void *array, int row, int col)
int **p, *object;
object= (int *) array;
if (p= (int * *) malloc (row * sizeof (int * *)))
int i;
for (i=0;i<row;i++)

return p;
printf ("No memory!/n");
Exit (1);
In which a void *array "Universal pointer" (which is too powerful) is responsible for receiving any two-dimensional array, coming in and then forcing it into a pointer to a one-dimensional array, which is assembled into an array of pointers based on row and Col, which returns the pointer.        With it, it's really much more convenient. This makes it possible to change the function that was written for a particular two-dimensional array without changing it to a function that can be used against any two-dimensional array.          Thanks to the "universal pointer" of void *, which is often called "null type Pointers" in books, I still find it better to call him a "generic pointer". If a function of the original output 3*3 a two-dimensional array element is changed slightly, it becomes a function that can output any two-dimensional array:
/* Display any dimension array */
void Display_array (void *array, int row, int col)
int **object_array, I, J;
Object_array=deal_array (array, row, col);
for (i=0;i<row;i++)
for (j=0;j<col;j++)
printf ("%5d", Object_array[i][j]);
printf ("n");
printf ("n");
Free (Object_array); }

       again, such as:  can only be used to find the diagonal of the 3*3 square, a little modification can become a function to find the diagonal of arbitrary square matrix.
int sum (void *array, int row, int col)
 int **object=null, I, j=0, sum=0;
 object=deal_array (array, row, col);
 for (i=0;i<row;i++)
  if (j==col-1-j)
   sum+=object[i][j ];
 return sum; 
}       can be tested in the main program: int main ()
{ Br> int a[5][5]={1,2,3,4,5,123,53,53,32,10,32,4,5,6,7,5,3,4,6,4,5,3,4,5}, b[4][4]={ 2,3,4,42,656,34,5,6,7,4,12,43,56,7,4};
 display_array (a,5,5);
 display_array (b,4,4);
 printf ("The sum:%d/n", SUM (A, 5, 5));
 printf ("The sum:%d/n", sum (b, 4, 4));
 return 0;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.