Arrays and pointers-pointers and multidimensional arrays

Why use pointers

  1. The value of a pointer is the address of the object it points to. The way the address is expressed depends on the hardware inside the computer. Many computers are addressing by byte, meaning that each byte in the memory is numbered sequentially. Here, a larger address is usually the address of the first byte of the object.
  2. Therefore, using pointers will be more efficient and can handle arrays well. The array representation is actually using pointers in disguise.

title

#include "stdio.h"
int main(){
    
    
     int data[4][2] ={
    
     {
    
    2,4}, {
    
    6,8}, {
    
    1,3}, {
    
    10,11}};
      return 520; 
}

For such a two-dimensional array, let's sort out the correspondence between a wave of pointers and the two-dimensional array:

 1. data是数组首元素的地址      因此data=&data[0]
 2. *data=data[0]          **data=data[0][0]
 3. data[1][2]=*(*(data+1)+2)

The first two conclusions are very simple, but the conclusion of the third question may be a bit difficult to understand. Now let’s analyze the third conclusion.

data          ← 二维数组首元素地址
data+1        ← 二维数组的第二个元素(即一维数组)的地址
*(data+1)     ←二维数组的第第二个元素(即一维数组)的首元素
*(data+1)+2   ←二维数组的第二个元素(即一维数组)的第三个元素(一个int类型的值)的地址
*(*(data+1)+2)←二维数组的第二个元素的第三个int类型元素的值

I wonder if you have made it clear up here?

This figure visually shows the array of arrays.
Insert picture description here

Pointer to multidimensional array

How to declare a pointer variable pz to point to a two-dimensional array? Such pointers are used when writing two-dimensional arrays like data, and it is not enough to declare the pointer to point to an int type. Because pointing to int can only match the type of data[0], indicating that the pointer points to a value of type int, but data is the address of his first element, and this element is a one-dimensional array containing two values ​​of type int, Therefore, pz must point to an array containing two int type values, not an int type value.

int (*pz)[2];      //pz指向一个内含两个int类型值的数组
int *pax[2];       //pax是一个内含两个指针元素的数组,每个元素都指向int类型的指针

What are the details of these two statements?

The former uses parentheses because [] has a higher priority than *, so * is combined with pz first, so it declares a pointer to an array (containing two values ​​of type int).

For the latter, since [] has a higher priority and is combined before pax, pax becomes an array containing two elements, and the asterisk indicates that the pax array contains two pointers. Finally, int indicates that the pointers in the pax array all point to values ​​of type int. Therefore, the latter declares two pointers to int.

I believe that after talking about this, everyone has another reference to the way of identifying the pointer.

Pointer compatibility

Assignment between numeric types:

int n=5;
double x;
x=n;     //隐式类型转换

Assignment of pointers:

int *pl=&n;
double &pd=&x;
pd=pl;            //编译时错误

Assignment between pointers is stricter than assignment between numeric types

About const in C and C++

The application of const in arrays is often defined in formal parameters to prevent the contents of the array from being modified.

#include "stdio.h"
void excute(const int a[]){
    
    
      /*.........   */
      /* 接下来的操作可以防止数组被修改  */
}

int main(){
    
    
    int a[100];
    /* ........... */
    excute(a);
    return 520;
}

The usage of const in C and C++ is very similar, but not exactly the same. One of the differences is that C++ allows the use of const integers when declaring the size of an array, while C does not. The second difference is that C++ pointer assignment checks are stricter:

const int y;
const int *p2=&y;
int *p1;
p1=p2;      //C++中不允许这么做,但是C可能只会给出警告

C++ does not allow assigning const pointers to non-const pointers. C allows this, but if you change y through p1, its behavior is undefined.

I hope this article will help you all, and you are welcome to correct me if you have any problems.

Guess you like

Origin blog.csdn.net/qq_42392049/article/details/112607002