the pointer to a structure

来源:互联网 发布:java多泛型转换 编辑:程序博客网 时间:2024/06/05 17:05
////////////////10.c#include<stdio.h>struct  student{int num;char *name;char *sex;}stu1;struct student stu[3]={{1,"student1","female1"},{2,"student2","female2"},{3,"student3","female3"}};int main()  {  struct student *p;struct student *pp;int i=0;for(p=stu;p<stu+3;p++,i++){printf("num:%d\n",p->num);printf("name:%s\n",p->name);printf("sex:%s\n",p->sex);}printf("\\\\\\\\\\\\\\\\\\\\\\change\\\\\\\\\\\\\n");pp=&stu[1].name;//the same as pp=(struct student*)&stu[1].name;//pp start from name filed of stu[1],not numprintf("name:%s\n",pp->name);//print name field--------but have printed sex field//pp->name -----is translated to----the first element of pp--------by compiler(++pp)->name="shit";//change the sex--------but have changed the sex fieldprintf("\\\\\\\\\\\\\\\\\\\\\\changed\\\\\\\\\\\\\n");for(p=stu;p<stu+3;p++,i++){printf("num:%d\n",p->num);printf("name:%s\n",p->name);printf("sex:%s\n",p->sex);}}
[root@localhost mmap]# gcc -o 10 10.c10.c: In function ‘main’:10.c:27: warning: assignment from incompatible pointer type[root@localhost mmap]# ./10num:1name:student1sex:female1num:2name:student2sex:female2num:3name:student3sex:female3\\\\\\\\\\\change\\\\\\name:female2\\\\\\\\\\\changed\\\\\\num:1name:student1sex:female1num:2name:student2sex:female2num:3name:student3sex:shit[root@localhost mmap]#