C language array element pointer

1. A variable has an address, and an array contains several elements. Each array element occupies a storage unit in memory, and they all have corresponding addresses.
2. Since a pointer variable can point to a variable, of course it can also point to an array element ( put the address of an element into a pointer variable ).
3. The pointer of the so-called array element is the address of the array element.
You can use a pointer variable to point to an array element. For example:

int a[10]={
    
    1,2,3,4,5,6,7,8,9,10};//定义a为包含10个整型数据的数组
int *p;//定义p为指向整型变量的指针变量
p=&a[0];//把a[0]元素的地址赋给指针变量p

The above code is to make the pointer variable p point to the 0th element of the a array.
4. Methods of referencing array elements:
(1) Subscript method, such as a[5].
(2) Pointer method, that is, to find the desired element through the pointer to the array element. Using the pointer method can make the target program of high quality (occupies less memory and runs faster).
5. In the C language, the array name (array name excluding formal parameters) represents the address of the first element in the array (the address of the element whose serial number is 0). Therefore the following two statements are equivalent:

p=&a[0];//p的值使a[0]的地址
p=a;//p的值是数组a首元素(即a[0])的地址

[Note]
In a program, the array name does not represent the entire array, but only the address of the first element of the array. The function of the above code p=a;is to assign the address of the first element of the array a to the pointer variable p instead of assigning the values ​​of each element of the array a to p.
6. A pointer variable can be initialized when it is defined. like:

int *p=&a[0];

It is equivalent to the following two lines:

int *p;
p=&a[0];

Of course you can also write:

int *p=a;

Its function is to assign the address of the first element of the a array (ie a[0]) to the pointer variable p (instead of *p).

Guess you like

Origin blog.csdn.net/NuYoaH502329/article/details/128996609#comments_25179279