Pointer 2 (I really can’t go into details)

One-dimensional arrays and pointers, pointer value and movement issues;

Table of contents

Basic introduction

Value and movement issues


Basic introduction

#include<stdio.h>
int main()
{
char  a[20]="China",b[20];
char *p1,*p2;
scanf("%s",b);//切记这里输入时,b前不要加'&'符号;
p1=a;
p2=b;
printf("%s,%s,%s,%s",p1,p2,a,b);//这里也是一样的。
return 0;
}

a represents the address of the first element in the array, and p points to the first address of the element in a;

There is such a relationship group a[2]=P[2]. They are all represented as the third element in the a array.

Because when inputting and outputting '%s' in a one-dimensional array string, the address of the first character is directly referenced, and then input and output in sequence  . Therefore, there is no need to add the '&' address character during input and output.

Remember not to add the '&' symbol . Although some compilers will not make errors, fundamentally this is not a correct format, because when re-entering, it is an address input, and b itself represents the first address of the first character, so If you add the address symbol again, there will be a conflict.

If we have to add the '&' character to get the address, we can run it like this;

int i;//定义一个整型变量i;
For(i=0;i<5;i++)
scanf("%c",&a[i])//因为这里表示a数组中第i个字符,所以可以加上取地址符依次赋值;

Because a represents the address of the first element, a+1 represents the address of the next element, and the same is true for p+1; it also represents the address of the next element, so when proceeding;

printf("%s",p+1);  //输出结果为hina;
print("%s",a+1);//同理;

After clearly understanding this concept, we can proceed to the next step of application

Because the address is a constant , we can perform address difference calculations.

Of course, the difference here means that the two pointers point to the same array , so that the difference can be calculated.

for example

int  a[2][3]={1,2,3,4,5,6},*p1,*p2;
p1=a[0];//让p1指向第一层第一个元素的地址;
p2=a[1];//让p2指向第二层第一个元素的地址;
printf("%d",p1-p2);

The output is: 3

Because pointers store addresses, the address of the same array can be the difference between the high and low addresses. If added, it actually doesn't make much sense, because you don't know which address unit the computer has allocated to it. Went in. Therefore, we only calculate the difference here.

Value and movement issues

The operation method of *p++ *(p++) is the same, because '*' and '()' are both unary operators and right associative; both mean that after taking the content of the address pointed to by p, p+1, also Just add one to the address. (For a string array, use p directly without the '*' symbol)

like:

char a[20]="CHINA";
char *p1;
p1=a;
printf("%s\n",p1++);
printf("%s",p1);

Output result:

CHINA

HINA

Here we should pay attention to the difference between *p++ and ( *p)++ (because ++*p and *++p are easier to understand, so we won’t explain too much) . One is to add one to the address after fetching the content . One is to take the content and add one to change the content. Let’s look at an example:

#include<stdio.h>
int main()
{
int a[3]={5,0,2},
b[3]={5,0,2},*p1,*p2;
p1=a;p2=b;
*p1++;(*p2)++;
printf("%d\n%d",*p1,*p2);        
}

The output result is: 0

                    6

       At this time, the pointer p1 points to a[1], which is 0, and the pointer p2 points to b[0]. The pointer p2 does not move, but changes the content it points to by adding one. Some friends may have questions here. If there are conditions such as assignment or operation, should assignment or addition be done first ? Here we are looking at an example;

int x,y,;
x=*p1++;y=(*p2)++;        
printf("%d  %d\n",x,y);
printf("%d  %d",*p1,*p2);

The output result is: 5 5

                    0  6

I believe that after seeing this, my friends should have the answer in their hearts. That is to do the assignment first, and then perform the addition operation . Because, if '++' is used first and then added after the variable , why does '*' run but '++' does not? Logically speaking, they are two operators of the same level. Why is this happening? ? Moreover, after the assignment is completed, (*p2)++; takes the content again and then adds it. Then, I came to the conclusion by asking @ZackSock master and several tests:

       We can ignore this '++' first , because as mentioned before, it is used first and then added , so we can ignore it first and look at other contents, which are all address contents, so , their values ​​are equal at the beginning. After the assignment is completed , the addition operation of the formula begins . Because the priority of '()' is higher than that of '++' , the content in the brackets will be added first , and then the addition operation will be performed. The last addition is the completion of execution. The content inside the brackets ;

       In the same way, if there is something with a higher priority than '()', it will be executed, and then it will be arranged in order of priority. Because '*' is just a simple value operator , p1 will also be executed. After adding and adding, the value is obtained, but there is no variable for it to assign, which means it is of no use;

To put it simply, the p1 pointer has moved , but the value it points to has not changed . The p2 pointer has not moved , but the value it points to has changed.

Okay, that’s all. Here is a small example for everyone to practice.

int x,y,z=3,i=3;
x=z++;y=(i=z)++;
printf("%d  %d\n",x,y);
printf("%d  %d",z,i);

Okay, that's the content of this chapter. If there is something wrong in the writing, please correct me and modify it. thank you all.

おすすめ

転載: blog.csdn.net/weixin_67118900/article/details/123604095