In depth parsing of array Pointers pointer arrays and two bit arrays

  • 2020-04-02 01:24:18
  • OfStack

Int *p[3] and int (*p)[3]
*p[3] this is a pointer array, which means that every element in the array is a pointer variable, while (*p)[3], p is a pointer variable, which means pointing to a one-dimensional array with three integer elements.


int i,j;
    int a[2][3]={3,4,5,6,7,8}; //
    int *p[3] ;  //Represents an array with three elements of pointer type
    int (*q)[3]; //Is a pointer to an array with three ints (q+1) that jumps three array elements
    //Store the first three element addresses in a p pointer array
    for( i=0;i<3;++i)
        p[i]=&a[0][i];
    //Output the corresponding value of the address in the pointer array
    for( j=0;j<3;++j)
        cout<< *p[j]<<" ";//The output is: 3,4,5
    cout<<endl;
    q=a;//Assign the starting address of array a to a one-dimensional array q;
    for(i=0;i<2;i++)
        for(j=0;j<3;j++)
            cout<< *(*(q+i)+j)<<" "; //Output the elements in the array
    system("pause");

Refer to c++ primer
Strictly speaking, there is no multidimensional array in c++, and the multidimensional array commonly referred to is actually an array of arrays, such as int arry[3][4]; Represents an array of length 3, and each element in the array is an array of length 4. Keeping this in mind when working with multidimensional arrays is helpful in understanding their application.

Let's talk about the relationship between multidimensional arrays and Pointers. As with normal arrays, when you use a multidimensional array, you actually automatically convert it to a pointer to the first element of that array. In other words, the name of the array is a pointer to the first element in the array, in one dimensional array, arry==&arry[0], the two addresses are the same. In a two-dimensional array, the array name points to the first element, which is an array of length 4. We define a pointer int (*p)[4] to an array of length 4, and then we can assign the first address of a two-dimensional array to it, p=arry. So you can assign. Again, arry==&arry[0].

Knowing the relationship between the names of two dimensional arrays and Pointers, then we will be easier to understand when we pass the parameters of two dimensional arrays, which has been a headache in the past. Here, we still pass the name of the two-dimensional array as an argument. In the parameter of the receiving function, we only need to define a pointer to the specific length of the array, for example, we use int (*p)[4] to accept parameters such as arry. An example of the code is shown below.


#include<iostream>
#include<stdlib.h>
using namespace std;
//The array name is a pointer to the first element of the array. Here we define a pointer to the array to accept arry
//R is the number of rows in a two-bit array, and c is the number of columns in a two-dimensional array.
void PrintArry(int (*arry)[4],int r,int c)
{
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
            cout<<arry[i][j]<<" ";
        }
        cout<<endl;
    }
}
void main()
{
    int arry[3][4]={{1,2,8,9},{2,4,9,12},{4,7,10,13}};
    PrintArry(arry,3,4);//Equivalent to PrintArry (& 'arry [0], 3, 4);
    system("pause");
}

The above is a simple example of printing a two-dimensional array, with emphasis on passing arguments to a two-dimensional array.

A more optimized approach
In the above example, the parameter must indicate that the arry pointer is pointing to an array of length, such as int (*arry)[4] must indicate that it is 4, there are some limitations, so is there a better way? The answer is yes. Considering that two-dimensional arrays occupy continuous space in memory, we can use them as arrays to represent two-bit arrays. Rewrite the above PrintArry method, and the result is as follows:


#include<iostream>
#include<stdlib.h>
using namespace std;
//Passing in a pointer to an array, the number of rows and columns in a two-dimensional array
void PrintArry2(int *arry,int r,int c)
{
    for(int i=0;i<r*c;i++)
    {
        cout<<arry[i]<<" ";
    }
    cout<<endl;
}
void main()
{
    int arry[4][4]={{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
    PrintArry2(&arry[0][0],4,4);//Pass in the address of the first element in the first array in the array
    system("pause");
}


Related articles: