C language learning: several commonly used representations of two-dimensional arrays

Name: Several representation methods of two-dimensional arrays

Note: The following two-dimensional array representation methods are commonly used:

The first is the representation of ordinary two-dimensional arrays.

The second is to use a one-dimensional array to represent a two-dimensional array. From the displayed element address, it can be seen that the storage method of the two-dimensional array and the two-dimensional array represented by the one-dimensional array in the memory is actually the same, but the two-dimensional array is used The array looks simpler, as long as there is no address conversion.

The third representation is to use an array of pointers. The elements in c[i] in this example are actually addresses. This method is more suitable when the size of each element is different. For example: Suppose there are several strings of unequal length that need to be processed by us. If a [i][j] structure is used, j must take the maximum length of these strings. Obviously this method wastes space.

If the *a[i] structure is adopted, only the first address of each string is stored in a, not the string itself, which not only saves space, but also reduces the time to directly manipulate the string.

The fourth way is to use pointer-to-pointer variables to represent. In this way, p is a pointer to a pointer, and the first address of an ordinary two-dimensional array (such as a in this example) cannot be assigned to it. (Because a is not a pointer to a pointer). The first address of the array pointer in the third method can be assigned to it. This method is less used.

#include<iostream>

using namespace std;

int main()

{

  int a[3][3] = {
   
   {0,1,2},{3,4,5},{6,7,8}};

  int b[9] = {0,1,2,3,4,5,6,7,8};

  int *c[3];

  int **p;

  cout<<"普通二维数组表示"<<endl;

  for(int i = 0;i<3;++i)

  {

    for(int j = 0;j<3;++j)

    {

      cout<<a[i][j]<<"("<<&a[i][j]<<")"<<" ";  //括号里是对应元素的地址

      //cout<<*(*(a+i)+j)<<" ";

    }

    cout<<endl;

  }

  cout<<"普通一维数组表示"<<endl;

  for(int i = 0;i<3;++i)

  {

    for(int j = 0;j<3;++j)

    {

      cout<<b[i*3+j]<<"("<<&b[i*3+j]<<")"<<" ";

    }

    cout<<endl;

  }

  cout<<"指针数组表示:"<<endl;

  for(int i = 0;i<3;++i)

  {

    c[i] = *(a+i);  //c[i]指向a数组的第i行首地址

    for(int j = 0;j<3;++j)

    {

      cout<<c[i][j]<<"("<<&c[i][j]<<")"<<" ";

    }

    cout<<endl;

  }

  cout<<"指针变量表示"<<endl;

  p = c;  //p为指向指针的指针,将指针数组c赋给指针变量p

  for(int i = 0;i<3;++i)

  {

    for(int j = 0;j<3;++j)

    {

      cout<<p[i][j]<<"("<<&p[i][j]<<")"<<" ";

    }

    cout<<endl;

  }

return 0;

}

 


In addition, if you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster! I may be able to help you here~

UP has uploaded some video tutorials on learning C/C++ programming on the homepage. Those who are interested or are learning must go and take a look! It will be helpful to you~

Sharing (source code, actual project video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning:

Programming learning:

 

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/115209912