zoukankan      html  css  js  c++  java
  • 结构体

    #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 ",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
    ",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
    ",st.name,st.age,st.sid);
    18     return 0;
    19 }

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

  • 相关阅读:
    博客最新博文通告
    博文快速导航
    创业
    央行回应中国版数字货币:与人民币等价 不会让钱贬值
    高屋建瓴
    高层人对事的处理
    老板的区别
    沟通的四大法则
    赚钱规则
    合伙做生意的原则
  • 原文地址:https://www.cnblogs.com/ajiaoa/p/14006512.html
Copyright © 2011-2022 走看看