On the difference between pointer array and array pointer in Go language

I have already introduced the usage of pointers in detail to you, so what is the change between the so-called pointer array and array pointer, and what is the connection between them? Whether it means one meaning or different meanings, let’s talk about it below.

Pointer array

First of all, it is an array. The elements of the array are pointers. How many bytes the array occupies is determined by the size of the array itself. Each element is a pointer. In a 32-bit system, any type of pointer always occupies 4 bytes. . It is short for "array of pointers."

Array pointer

First, it is a pointer, which points to an array. In a 32-bit system, any type of pointer always occupies 4 bytes. As for how many bytes the array points to, I don’t know, it depends on the size of the array. It is short for "pointer to array".

When the pointer is at the back, it means it is a pointer, just pointing to an array;
when the pointer is at the front, it means it is an array, but the elements of the array are all pointers.
So everyone should be able to understand what I'm talking about.

definition

Array pointer (also called row pointer) definition

int (*p)[n]

() The priority is high. First, p is a pointer, pointing to an integer one-dimensional array. The length of this one-dimensional array is n, which can also be said to be the step size of p. That is to say, when p+1 is executed, p has to span the length of n integer data.

//定义二维数组
 int a[3][4];

//该语句是定义一个数组指针,指向含4个元素的一维数组。
 int (*p)[4]; 
      
//将该二维数组的首地址赋给p,也就是a[0]或&a[0][0]
 p=a;  
    
//该语句执行过后,也就是p=p+1;p跨过行a[0][]指向了行a[1][]
 p++;   

Therefore, array pointers are also called pointers to one-dimensional arrays, or row pointers.

In the two-dimensional array
, the five values ​​of a,&a,a[0],&a[0],&a[0][0] are the same, and they all point to the first address (starting position) of the two-dimensional array. The
difference lies in these five The types of expressions are different, take int a[2][3] for example:

a is the name of the two-dimensional array, which is a constant, and stores the first address of the two-dimensional array. The type is a two-dimensional array, sizeof(a)=24

&a The (first) address of the two-dimensional array a, which itself is a pointer, sizeof(&a)=4, the pointer type is 4

a[0] points to the first row of the two-dimensional array a[0][], the type is a one-dimensional array sizeof(a[0])=12
&a[0] represents the address of a[0], and one-dimensional The array name a[0] has the same value, but it is a pointer type sizeof(&a[0])=4
&a[0][0] represents the address of the first element in a two-dimensional array, and the pointer type sizeof(&a[0] [0])=4

Pointer array definition

 int *p[n]

[] High priority, first combine with p to form an array, and then int* shows that this is an integer pointer array, which has n pointer type array elements. When p+1 is executed here, p points to the next array element.

The following assignment is wrong:

p=a;

Because p is an unknowable representation, only p[0], p[1], p[2]...p[n-1] exist, and they are pointer variables that can be used to store variable addresses.
But it can be assigned as follows:

*p=a;

Here *p represents the value of the first element of the pointer array, which is the value of the first address of a.

To assign a two-dimensional array to a pointer array:

int *p[3];
int a[3][4];
p++; //该语句表示p数组指向下一个数组元素。注:此数组每一个元素都是一个指针
for(i=0;i<3;i++)
p[i]=a[i]

Here int *p[3] means that there are three pointer variables stored in a one-dimensional array, which are p[0], p[1], p[2]; therefore, assign values ​​separately.

The array pointer is just a pointer variable. It seems to be used specifically to point to a two-dimensional array in the C language. It occupies the storage space of a pointer in the memory.

The pointer array is a number of pointer variables, stored in the memory in the form of an array, occupying the storage space of multiple pointers.
Another point that needs to be explained is that when it is used to point to a two-dimensional array at the same time, the reference is the same as the reference by the array name.
For example, to represent an element in row i and column j in the array:

*(p[i]+j)*(*(p+i)+j)(*(p+i))[j]、p[i][j]

Priority: ()> []> *

Judge pointer array and array pointer according to priority

Which of the following is an array pointer and which is an array of pointers?

A)
int *p1[10];

B)
int (*p2)[10];

Before that, you need to understand the priority of a symbol.

First analyze the priority of "[]" in A) than "*".
p1 is first combined with "[]" to form the definition of an array, the array name is p1, int * modifies the content of the array, that is, each element of the array. So now we can understand that this is an array, which contains 10 pointers to int type data, that is, an array of pointers.

Reanalysis B) "()" has a higher priority than "[]".
The "*" sign and p2 constitute the definition of a pointer. The pointer variable is named p2. Int modifies the content of the array, that is, each element of the array. The array does not have a name here, it is an anonymous array.
Now we know that p2 is a pointer, which points to an array containing 10 int data, that is, an array pointer.

As shown below:
Insert picture description here

On the difference between a and &a

To illustrate the difference between a and &a, the code is as follows:

int main()
{
    
    
   char a[5]={
    
    'A','B','C','D'};
   char (*p3)[5] = &a;
   char (*p4)[5] = a;
   return 0;
}

What will be the value of p3+1? What will be the value of p4+1? There is no doubt that p3 and p4 are array pointers, pointing to the entire array.
&a is the first address of the entire array;
a is the first address of the first element of the array .

The value is the same but the meaning is completely different.
In C language, the data types on both sides of the assignment symbol "=" must be the same. If they are different, explicit or implicit type conversion is required.

The data types on both sides of the "=" in the definition of p3 are exactly the same, while the data types on both sides of the "=" in the definition of p4 are inconsistent.
The type on the left is a pointer to the entire array, and the data type on the right is a pointer to a single character.

Now that it is clear that both p3 and p4 point to the entire array, the values ​​of p3+1 and p4+1 are easy to understand.

What's the problem if you change the length of the array to be smaller? What are the values ​​of p3+1 and p4+1?

int main()
{
    
    
   char a[5]={
    
    'A','B','C','D'};
   char (*p3)[3] = &a;
   char (*p4)[3] = a;
   return 0;
}

What's the problem if the length of the array is increased?

int main()
{
    
    
   char a[5]={
    
    'A','B','C','D'};
   char (*p3)[10] = &a;
   char (*p4)[10] = a;
   return 0;
}

What are the values ​​of p3+1 and p4+1 at this time?

The results obtained:
(1). char (*p2)[5]=a;
A forced conversion must be used, such as:

char (*p2)[5]=(char (*)[5])a;

(2). If the length of the array is changed, the error message that the compilation fails:

error C2440: 'initializing' : cannot convert from 'char (*)[5]' to 'char (*)[3]'

error C2440: 'initializing' : cannot convert from 'char (*)[5]' to 'char (*)[10]'

(3). After changing the above program, the code to run successfully is as follows:

int main()

{
    
    

  	char a[5]={
    
    'a','b','c','d'};

  	char (*p1)[5]= &a;

  	char (*p2)[5]=(char (*)[5])a;


  	printf("a=%d\n",a);

  	printf("a=%c\n",a[0]);

  	printf("p1=%c\n",**p1);

  	printf("p2=%c\n",**p2);

  	printf("p1+1=%c\n",**(p1+1));

  	printf("p2+1=%c\n",**(p2+1));


  	return 0;

}

operation result:

a=1638208

a=a

p1=a

p2=a

p1+1=?

p2+1=?

Press any key to continue

You can try the results later and see what you get.

in conclusion:

According to the pointer type and the object pointed to, it indicates the size of the pointer, and each time it is increased by 1, it means that the size of the pointer is increased in bytes.

Guess you like

Origin blog.csdn.net/zp17834994071/article/details/108716755