Understand the c language array type

Throwing bricks: Array type, the three mountains that crush beginners
1. Array type; Array name
2. Array pointer; Is the same level as the array name or the upper level requires & to be assigned
3. The relationship between array type and array pointer;

Let’s take a one-dimensional array as an example; int a[10]; int *a1; int (*a2)[10];
a; the first address of the a array is a pointer and the type pointed to is int, then a can be equivalent Compared with a1. The difference is that a is a constant and cannot be modified a = a+1; this modification, and a1 is a variable, both can be added and subtracted.
a1; is a pointer to an int type.
a2; In fact, a pointer points to an array type of int [10]. Therefore, a2 = &a; so that the data types are aligned.

In the case of two dimensions, int b[][10];int ( b1)[10]; Similarly, b is a constant and cannot be changed, and b1 is a variable.
b; b is the first address of the array, that is, the pointer points to the array of int [10], so the basic unit of addition and subtraction is 40 bytes.
Therefore, b+1 is the first address of the second row, and
(b+1) is the first address of the second row of the array. Then take * is the value of the array.
And the type pointed to by b1 is the same as b, so two can be b1 = b;

#include <stdio.h>

int main()
{
	int b = 10;
	int a[10]={1,5,3,4,10,6,7,8,9,0};
	int a1[][10]={
					{1,2,3,4,5,6,7,8,9,0},
					{10,2,3,4,5,6,7,8,9,0}
				};
	printf("%d\n",b);//b变量做右值表示取值 
	b = 12; //b变量做左值表示操作b空间。 
	printf("%d\n",*(a+1)); //a是一个地址,其加法的基本单位是int(因为下一层次是int类型)加4再取值就是第二个元素 
	printf("%d\n",(*a+1));//先取值再加1.
	
	//同理理解二维数组。
	//a1+1;则表示跳转到第二行,表示第二行的首地址,是第二行的地址而不是第二行数组的首地址
	//*(a1+1);这个才表示第二行数组的首地址。打印的值是一样的,但是其数据类型不是一样的。 
	printf("%d\n",*(*(a1+1)+2));
	以上是数组类型的测试/
	///下面是关于数组指针类型的测试///
	int *p = NULL;
	int (*p1)[10] = NULL;//优先级问题,* []是同一等级再从右往左,因此要打括号。 
	p = a;
	printf("%d\n",*(p+1));
	p1 = &a;
	printf("%d\n",*((*p1+1)+1));
	p1 = a1;
	printf("%d\n",*((*p1+1)+1));
	return 0;
}

Guess you like

Origin blog.csdn.net/zw1996/article/details/84705948