the pointer to a structure

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 num

printf("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 field

printf("\\\\\\\\\\\\\\\\\\\\\\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.c
10.c: In function ‘main’:
10.c:27: warning: assignment from incompatible pointer type
[root@localhost mmap]# ./10
num:1
name:student1
sex:female1
num:2
name:student2
sex:female2
num:3
name:student3
sex:female3
\\\\\\\\\\\change\\\\\\
name:female2
\\\\\\\\\\\changed\\\\\\
num:1
name:student1
sex:female1
num:2
name:student2
sex:female2
num:3
name:student3
sex:shit
[root@localhost mmap]# 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值