结构体

#include<stdio.h>
//2020年11月19日16:46:44
//修改结构体变量成员的值-两种方式
struct Student
{
int age;
float score;
char sex;
};
int main(void)
{
struct Student st = {11,23.3,'F'};//定义的同时赋初值;这是第一种方式
//但一般不使用,因为不可能为每一块都分配一个名字,太麻烦
st.score=66.6f;//第一种方式,66.6在C语言中默认是double类型,
//如果希望它是float类型,需要在末尾加f或者F

struct Student * pst = &st;//第二种方式,用一个指针变量指向了结构体变量
//何为指向?即指针变量存储了结构体变量的地址
pst->age=88;//pst->age等价于(*pst).age等价于st.age
//这个代码的意思是pst所指向的结构体变量中的age这个成员
printf("%d,%f\n",st.age,st.score);
return 0;
}

 


 1 #include<stdio.h>
 2 #include<string.h>
 3 struct Student
 4 {
 5     char name[100];
 6     int age;
 7     int sid;
 8 };
 9 int main(void)
10 {
11     struct Student st={"zhangsan",10,23};
12     printf("%s,%d,%d\n",st.name,st.age,st.sid);
13     //st.name="zhangsan";//error,name代表的首元素地址
14     strcpy(st.name,"zhangsan");
15     st.age=10;
16     st.sid=23;
17     printf("%s,%d,%d\n",st.name,st.age,st.sid);
18     return 0;
19 }

注意字符数组的赋值方式哈

 

posted @ 2020-11-19 16:49  Connor_Jiao  阅读(138)  评论(0编辑  收藏  举报