#include<stdio.h>
#include<string.h>
struct Student{
int sid;
int age;
char name[200];
}; //分号不能省
int main(void){
struct Student st = {1000,20,"zhangsan"};
struct Student *pst ; //pst是一个存放struct Student类型地址的变量
printf("%d,%s,%d\n",st.sid,st.name,st.age);
//输出结果1000,zhangsan,20
//printf("%d,%s,%d\n",st); ///error 输出结果6421632,(null),0
st.sid = 99; //第一种赋值方法
//st.name = "zhangsan"; //error
strcpy(st.name,"zhangsan");
st.age = 22;
pst = &st;
pst->sid = 99; //pst->sid等价于(*pst).sid,所以pst->sid等价于st.sid
printf("%d,%s,%d\n",st.sid,st.name,st.age);
//输出结果99,zhangsan,22
return 0;
}
【数据结构】结构体赋值
最新推荐文章于 2024-09-04 17:06:22 发布