1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 结构体的赋值和初始化与取出结构体变量中的成员

结构体的赋值和初始化与取出结构体变量中的成员

时间:2020-12-14 01:34:28

相关推荐

结构体的赋值和初始化与取出结构体变量中的成员

1/*结构体的赋值和初始化*/

2

3#include<stdio.h>

4

5structStudent

6{

7intage;

8floatscore;

9charsex;

10};

11

12intmain(void)

13{

14structStudentst={80,66.6,'F'};//定义同时就赋值

15structStudentst2;//下一行不能写一句类似于st2={10,88,'M'};的语句,除非定义时就赋值。

16st2.age=10;

17st2.score=88;

18st2.sex='M';

19

20printf("%d,%f,%c\n",st.age,st.score,st.sex);

21printf("%d,%f,%c\n",st2.age,st2.score,st2.sex);

22

23return0;

24}

25/*

26在Vc++6.0中显示的结果是:

27=========================================

2880,66.599998,F

2910,88.000000,M

30=========================================

31*/

1/*

2如何取出结构体变量中的每一个成员

3*/

4#include<stdio.h>

5

6structStudent

7{

8intage;

9floatscore;

10charsex;

11};

12

13intmain(void)

14{

15structStudentst={80,66.6F,'F'};

16printf("age=%d\n",st.age);

17

18

19structStudent*pst=&st;//&st不能改成st

20pst->age=88;//第二种方式。。。pst->age在计算机内部,会被转化成(*pst).age这是一种硬性规定

21//所以pst->age等价于(*pst).age,也等价于st.age

22printf("age=%d\n",st.age);

23

24

25st.age=10;//第一种方式

26printf("age=%d,score=%f\n",st.age,pst->score);//st.age可写成pst->age,pst->score也可写成st.score.

27

28return0;

29}

30/*

31在Vc++6.0中显示的结果是:

32==============================================================

33age=80

34age=88

35age=10,score=66.599998

36==============================================================

37*/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。