首页 > 其他 > 详细

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

时间:2016-07-01 14:45:19      阅读:185      评论:0      收藏:0      [点我收藏+]
#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,此处为验证上一步的指针位置。

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

原文:http://www.cnblogs.com/iloverain/p/5632985.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!