验证 结构体指针与自增运算符

#include <stdio.h>
#include <stdlib.h>

int main() {

 struct student {
  char *name;
  int score;
 };

 struct student st = {"Brian", 97};
 struct student *ptr = &st;

 printf("ptr->name = %s\n", ptr->name);
 printf("*ptr->name = %c\n", *ptr->name);
 printf("*ptr->name++ = %c\n", *ptr->name++);//获取首地址字符后,将name指针友谊一位,指向r
 printf("*ptr->name = %c\n", *ptr->name);
 printf("ptr->score = %d\n", ptr->score);
 printf("ptr->score++ = %d\n", ptr->score++);
 printf("ptr->score = %d\n", ptr->score);
 return 0;
}


1. ptr->name,等同于打印(*p).name。

2. *ptr->name,因为->的优先级高于*,所以相当于: *(ptr->name)。即指针首地址的那个字符。

3. *ptr->name++,由于*和++的优先级相同,而且结合性是由右至左,所以相当于: *((ptr->name)++),即获取首地址字符后,将name指针右移一位。(当前打印还是首地址的值)

4. *ptr->name,此处为验证上一步的指针位置。

posted @ 2016-07-01 14:34  QualityAssurance21  阅读(1711)  评论(0编辑  收藏  举报