832结构体变量的初始化 结构体变量初始化就是在定义结构体变量的同时给 变量提供初值,就一般的变量而言实际上是给变量 的成员赋初值 1)给结构体指针变量赋初值 2)给结构体变量赋初值 3)给结构体数组赋初值
8.3.2 结构体变量的初始化 结构体变量初始化就是在定义结构体变量的同时给 变量提供初值,就一般的变量而言实际上是给变量 的成员赋初值。 ❖ 1)给结构体指针变量赋初值 ❖ 2)给结构体变量赋初值 ❖ 3)给结构体数组赋初值
结构体变量初始化,并输岀其初始值。 令# include< stdio.h> struct student i int num: char name [151 char sex Int ager float score: }st1={20001," Wang Hong""F,2095.5} maino i printf("student stI\n"); printf("%d, %S, %C, %d, %5.1f\n",st1.num, stl name stl. sex,sti. age, stI score);]
结构体变量初始化,并输出其初始值。 ❖ #include<stdio.h> struct student { int num; char name[15]; char sex; int age; float score; }st1={20001,"WangHong",'F',20,95.5}; main() { printf("student st1\n"); printf("%d,%s,%c,%d,%5.1f\n",st1.num,st1.name, st1.sex,st1.age,st1.score);}
运行结果 C:\E,C,test Debug\test.exe student st1 20001,WangHong, F,20, 95.5 Press any key to continue
运行结果:
8.3.3结构体变量的使用 ◇注意:结构体变曩只允迕整体赋值,其他操作如赋值、输入、输出、运算等 必须通过引用结构体变量的成员进行相应的操作。 冷【例8-4】结构体变量(整体)使用示例。 include<stdio h> struct Student I char name[20] float score maino i struct Student wa=ppp, 98.0), wb= W, 68.01 struct Student*p, w 3=kkk, 88.0: M们]=wa;/*结构体变量(整体)赋值 M2]=wb;/结构体变量(整体)赋值 printf("w[0]: %s, %f\n", wO] name, w[0]. score) p=&w1 printf("w1}:%s,%fn"、(p).name.、(p). score) p=&w[2] printf("w2]: %S, %f\n",p->name, p->score): 1
8.3.3结构体变量的使用 ❖ 注意:结构体变量只允许整体赋值,其他操作如赋值、输入、输出、运算等 必须通过引用结构体变量的成员进行相应的操作。 ❖ 【例8-4】结构体变量(整体)使用示例。 #include<stdio.h> struct Student { char name[20]; float score;}; main() { struct Student wa={"ppp",98.0},wb={"WWW",68.0}; struct Student *p,w[3]={"kkk",88.0}; w[1]=wa; /*结构体变量(整体)赋值*/ w[2]=wb; /*结构体变量(整体)赋值*/ printf("w[0]:%s,%f\n",w[0].name,w[0].score); p=&w[1]; printf("w[1]:%s,%f\n",(*p).name,(*p).score); p=&w[2]; printf("w[2]:%s,%f\n",p->name,p->score);}
运行结果: c E:\C\test\ Debug\test.exe 口 w[B]:kkk,88.99999 v[11:ppp,98.0008 w[2:WW.68.99999 Press any key to continue
运行结果: