Two-dimensional arrays do function parameters, pointers to pointers do function parameters

Source: Internet
Author: User

Here is an article written very well http://blog.csdn.net/f81892461/article/details/8974087

The article has a sentence in short: the array can not be used as a parameter, always by the compiler will assign the address to the parameter pointer, even if the pointer is defined in the form of arrays, but also pointers. The differences are then caused by the way the array and pointers are positioned in different elements.

Well, it's a good thing to say that arrays do formal parameters that are treated as pointers. Do not understand this sentence can look, refer to the article link in the picture, pay attention to compare the two: Data+1, the first data+1 points to the address of the element data[1][0], and the second data+1 point to the element data[0][4], why? This is because the data array is treated as a pointer when it is a parameter, and any type of pointer in a 32-bit array takes up 4 bytes, so data+1 and data are 4 bytes apart.

Stick to the code in the article

#include <stdio.h>; #include<stdlib.h>;voidFunChar**data);intMainvoid){       Chardata[2][5]; printf ("In main\n"); printf ("data:%p\n", data); printf ("*data:%p\n", *data); printf ("data+1:%p\n", data+1); printf ("* (data+1):%p\n", * (data+1)); printf ("data[1]:%p\n", data[1]); printf ("&data[1][3]:%p\n", &data[1][3]); Fun ((Char**) data);       GetChar (); Exit (0);}voidFunChar**data) {printf ("In fun\n"); printf ("data:%p\n", data); printf ("*data:%p\n", *data); printf ("data+1:%p\n", data+1); printf ("* (data+1):%p\n", * (data+1)); printf ("data[1]:%p\n", data[1]); printf ("&data[1][3]:%p\n", &data[1][3]); return ;}

So, what if the method in the above code doesn't work?

Read another article http://blog.csdn.net/xinshen1860/article/details/20620227

The article mentions:

int data[3[4] = {{1234}, {55 7 8}, {9-ten}}int3);
int int (*arr) [4int  size]; // the parentheses are necessary because the following declaration declares an array of four pointers to int instead of a pointer to an array of 4 int.  int *arr[4];   // declares an array of pointers that contains 4 int pointer variables int (*arr) [4// declares a pointer variable, which points to an array of 4 int int sum (int arr[][4], int size);

There is another declarative format that is exactly the same as the correct prototype, but more readable:

int sum (int arr[][4int size);

There is also an article http://blog.163.com/[email protected]/blog/static/165747821201052195212719/

This paper discusses the function parameters of one-dimensional array and two-dimensional array, gives the code, and discusses the reference of the two-dimensional array, and points out

 int  Fun (int  (&a) [2 ][3 ]) // ok, reference as formal parameter, array as reference parameter, must be specified clearly all dimensions  //  int fun (int (&a) [][3],int N)  // error C2265: ' <Unknown> ': reference to a zero-sized array is illegal  //  int fun (int &a[2][3])  //  error C2234: ' <Unknown> ': Arrays of references is illegal
     

But notice the fact that it is a pointer to a pointer in itself: such as the following code, the code to the word "sword refers to the offer", the completion of the function of the clockwise printing matrix, in the test function, it directly using the pointer of the application space, in the function of the parameter list is also directly in the form of pointers pointer , when calling other functions in the test function, it can be called directly, such as Printmatrix (numbers, columns, rows); This line of code.

voidPrintmatrixincircle (int**a,intColumnsintRowsintstart) {    intendx=columns-1-start; intendy=rows-1-start; //print a row from left to right     for(inti=start;i<=endx;i++)    {        intNumber=A[start][i]; cout<<number<<"  "; }    //print a line from top to bottom    if(start<EndY) {         for(inti=start+1; i<=endy;i++)        {            intNumber=A[i][endx]; cout<<number<<"  "; }    }    //print a row from right to left    if(start<endx&&start<EndY) {         for(inti=endy-1; i>=start;i--)        {            intNumber=A[endy][i]; cout<<number<<"  "; }    }    //print a line from bottom to top    if(start<endx&&start<EndY) {         for(inti=endy-1; i>=start+1; i--)        {            intNumber=A[i][start]; cout<<number<<"  "; }} cout<<Endl;}voidPrintmatrix (int**a,intColumnsintRows//Rows and Columns{    if(a==null| | columns<=0|| rows<=0)        return; intstart=0;  while(columns>start*2&&rows>start*2) {printmatrixincircle (A,columns,rows,start); Start++; }}//==================== test Code ====================voidTest (intColumnsintRows//Row row column columns{printf ("Test Begin:%d columns,%d rows.\n", columns, rows); if(Columns <1|| Rows <1)        return; int* * numbers =New int*[rows];//the * after int is not multiplication sign is a pointer operator     for(inti =0; i < rows; ++i) {numbers[i]=New int[columns];  for(intj =0; J < columns; ++j) {Numbers[i][j]= i * columns + j +1; cout<<numbers[i][j]<<"  "; }} cout<<Endl;    Printmatrix (Numbers, columns, rows); printf ("\ n");  for(inti =0; i < rows; ++i)Delete[] (int*) numbers[i]; Delete[] numbers;}

Two-dimensional arrays do function parameters, pointers to pointers do function parameters

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: info-contact@alibabacloud.com 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.