zoukankan      html  css  js  c++  java
  • c语言学习笔记(10)——结构体

    ------------------------------------------------------------------
    # include <stdio.h>
    struct Student{  //Student可以看成一种数据类型
    int age;
    float score;
    char sex;
    };
    int main(void){
    struct Student st = {80,66.6,'F'};


    return 0;
    }
    ------------------------------------------------------------------
    一、为什么需要结构体?
    为了表示一些复杂的事物,而普通类型无法满足实际需求
    二、什么叫结构体?
    把一些基本类型组合在一起形成的一个新的复合数据类型叫做结构体。
    三、如何定义一个结构体?
    第一种方式
    struct Student{  
    int age;
    float score;
    char sex;
    };
    第二种方式
    struct Student2{
    int age;
    float score;
    char sex;
    } str2;
    第三种方式
    strut {
    int age;
    float score;
    char sex;
    } str3;
    四、怎么去使用结构体变量
    1.赋值和初始化
     定义的同时可以整体赋初值
     如果定义完后,则只能单个的赋初值
    --------------------------------------------------------------------
    # include <stdio.h>


    struct Student
    {
    int age;
    float score;
    char sex;
    }
    int main(void){
    struct Student st = {80, 66.6, 'F'};  //整体赋值
    struct Student st2;  //单个赋值
    st2.age = 10;
    st2.score = 88;
    st2.sex = 'F';

    printf("%d %f %c ", st.age, st.score, st.sex);  //第一种取值方式
    printf("%d %f %c ", st2.age, st2.score, st2.sex);

    return 0;
    }
    --------------------------------------------------------------------

    2.如何取出结构体变量中的每一个成员
    1.结构体变量名.成员名
    2.指针变量->成员名
    -------------------------------------------------------------------------
    # include <stdio.h>


    struct Student
    {
    int age;
    float score;
    char sex;
    };
    int main(void){
    struct Student st = {40, 60, 'F'};
    struct Student * pst = &st;


    pst->age = 68;
    st.score = 66;
    printf("%d %f ", st.age, pst->score);

    return 0;
    }
    -------------------------------------------------------------------------
    pst->age在计算机内部会被转化成(*pst).age
    3.结构体变量的运算
    结构体变量不能相加,不能相减,也不能相互乘除但结构体变量可以相互赋值
    --------------------------------------------------------------------------
    struct Student
    {
    int age;
    char sex;
    char name[100];
    };  //分号不能省略
    struct Student st1, st2;
    st1+st2  st1*st2  st1/st2 都是错误的
    st1 = st2  或者 st2 = st1 都是正确的
    ---------------------------------------------------------------------------
    4.结构体变量和结构变量指针作为函数参数传递的问题
    推荐使用结构体指针变量作为函数参数来传递
    -------------------------------------------------------------------------
    # include <stdio.h>


    struct Student
    {
    int age;
    float score;
    char sex;
    };


    int main(void){
    struct Student st;

    InputStudent(&st);  //对结构体变量输入
    OutputStudent(&st);  //对结构体变量输出

    return 0;
    }
    void OutputStream(struct Student * ss){
    printf("%d %c %s ", ss->age, ss->sex, ss->name);
    }
    void InputStudent(struct Student * pstu){ //pstu只占4个字节
    (*pstu).age  = 10;
    strcpy(pstu->name, "zhangsan");
    pstu->sex = 'F';
    }
    ---------------------------------------------------------------------------
    5.动态的构造存放学生信息的结构体数组
    ---------------------------------------------------------------------------
    /*
    2012年2月5日19:43:24
    */
    # include <stdio.h>
    # include <malloc.h>


    struct Student
    {
    int age;
    float score;
    char name[100];
    };
    int main(void){
    int len;
    struct Student * pArr;
    int i;

    printf("请输入学生的个数: ");
    printf("len = ");
    scanf("%d", &len);
    pArr = (struct Student *)malloc(len * sizeof(struct Student));


    for (i=0; i<len; ++i){
    printf("请输入第%d个学生的信息 ", i+1);
    printf("age = ");
    scanf("%d", &pArr[i].age);


    printf("name =");
    scanf("%s", pArr[i].name);


    printf("score = ");
    scanf("%f", &pArr[i].score);
    }
    //输出
    printf(" ");
    for (i=0; i<len; ++i){
    printf("第%d个学生的信息是 ", i+1);
    printf("age = %d ", pArr[i].age);


    printf("name = %s ", pArr[i].name);


    printf("score = %f ", pArr[i].score);
    }

    return 0;
    }
    输出结果:
    请输入学生的个数:
    len = 3
    请输入第1个学生的信息
    age = 22
    name =李小强
    score = 99
    请输入第2个学生的信息
    age = 23
    name =杨鹏
    score = 89
    请输入第3个学生的信息
    age = 24
    name =王海涛
    score = 88






    第1个学生的信息是
    age = 22
    name = 李小强
    score = 99.000000


    第2个学生的信息是
    age = 23
    name = 杨鹏
    score = 89.000000


    第3个学生的信息是
    age = 24
    name = 王海涛
    score = 88.000000
    ---------------------------------------------------------------------------

    链表
  • 相关阅读:
    [LeetCode] 771. Jewels and Stones
    [LeetCode] 129. Sum Root to Leaf Numbers
    java定时器demo
    Spring Boot与监控管理
    springboot与热部署
    springboot中的web项目不能访问templates中的静态资源
    @Component 和 @Bean 的区别
    springcluoud入门
    Dubbo和Zookerper的关系
    Spring boot配置Dubbo三种方式
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6469915.html
Copyright © 2011-2022 走看看