关于指针

关于第一句printf语句:p++->n,由于P开始指向结构体数组a的首地址,那么p++->n,在这里存在一个运算符的优先级的问题,p++->n先取得P当前指向的结构体的N,再将P指向下一个元素,然而++p->n再取得P当前指向的结构体的N,再将这个值+1。那么题目中p++->n,等价于p->n,p=p+1.先访问p指向的结构体中的数据,然后再让P指向下一行的元素的地址。在此时,如果改成(p++)->n,那么结果是一样的,括号的优先级高于->.但是,此时也存在前加和后加的问题。

第二条printf语句:p->n++,等价于p->n,n=n+1

第三条printf语句:(*p).n++

拓展:结构体变量的引用    

C语言规定了两种运算符可以用于访问结构体成员:一种是成员运算符,也称圆点“.”运算符;一种是指向运算符,也称箭头运算符“->”.

下面引用一个实例说明二者的不同:

 1 #include<stdio.h>
 2 int main()
 3 {
 4   struct student
 5   {
 6     char name[20];
 7     char sex;
 8     int age;
 9     float score;    
10   }stu;
11  
12   printf("输入姓名:
");
13   gets(stu.name);
14   printf("输入性别:
");
15   stu.sex = getchar();
16   printf("输入年龄:
");
17   scanf("%d",&stu.age);
18   printf("输入成绩:
");
19   scanf("%f",&stu.score);
20  
21   printf("姓名:%s,性别:%c,年龄:%d,成绩:%5.2f
",stu.name,stu.sex,stu.age,stu.score);
22   system("pause"); 
23   return 0;
24 }
25 
26  
27 
28 #include<stdio.h>
29 int main()
30 {
31   struct student
32   {
33      char number[6];
34      char name[20];
35      char sex;
36      int age;
37      float score;       
38   }s1={"12004","李明",'m',19,298.3},s2={"12005","王丽",'f',18,227.9};
39   struct student *p;  //定义p为结构体变量
40   p = &s1;          //p指向结构体变量s1
41   printf("学号       姓名        性别      年龄      分数

");
42   printf("%s     %s    %c    %d     %5.2f
",p->number,p->name,p->sex,p->age,p->score);
43   p = &s2;        //p指向结构体变量s2
44   printf("%s %s    %c%d   %5.2f
",p->number,p->name,p->sex,p->age,p->score);
45   system("pause"); 
46   return 0;
47 }

第四条printf语句:++p->n,等价于p->n,p指向的内容此刻为:*p+1,原来内容加1。

原文地址:https://www.cnblogs.com/xuyinghui/p/4909470.html