Pointer problem in C language two-dimensional array

Take a two-dimensional array a[3][2] as an example to briefly introduce the problem of pointers in a two-dimensional array

1. Basic meaning

1.a[3][2] represents a two-dimensional array with three rows and two columns, a[0], a[1], a[2] represent the zeroth row, the first row, and the first element of the second row, respectively Of 地址.
2. The array name a is essentially a pointer to a one-dimensional array ( 数组指针), in this case the zeroth row of the a array.
Note: The meaning of the array name a is also equivalent to &a[0]

2. The difference between &a[0] and a[0]:

The address values represented by both are the same , but their types and meanings are different.

&a[0] and a[0] both point to the first address of the element in the zeroth row and zeroth column of the a array , but &a[0] is essentially one 数组指针; and a[0] is essentially one 指向int类型的指针.

From this we can deduce: &a[0]+1 and a[0]+1 also have different meanings:

&a[0]+1 points to the element address of the first row and zeroth column of the a array, and a[0]+1 points to the element address of the zeroth row and first column of the array.

3. Application of value operator * in two-dimensional array

For pointer variables that point to basic data, such as a[1]+1, which *(a[1]+1)represents the value of the address element;

In this case, the &a[1]+1或a+2result of the value operator represents the address of the zeroth element of the row, which is of type int*.

In summary, it is a sentence: when the pointer to the basic data type is taken, the return result is the corresponding value, and when the pointer of other types is taken, the return result is downgraded to the corresponding pointer variable.

4. Relevant test code

#include <stdio.h> 

int main()
{
    
    
	int a[3][2] = {
    
    1,2,3,4,5,6};
	printf("a = %p\n&a[0] = %p\na[0] = %p\n",a,&a[0],a[0]);
	printf("-------------------\n");
	printf("&a[0] + 1 = %p\na[0] + 1 = %p\n",&a[0] + 1,a[0] + 1);
	printf("-----取值运算符-----\n");
	printf("*(a[1] + 1) = %d\n&a[1] + 1 = %p\na + 2 = %p\n",*(a[1] + 1),&a[1] + 1,a + 2);
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_46550452/article/details/108954271