zoukankan      html  css  js  c++  java
  • [C]结构Struct与指针,函数

     指针传递,不返回值

    #include <stdio.h>
    #include <string.h>
    struct Student
    {
        char name[10];
        int age;
        struct subjects {
            double math;
            double english;
            double science;
        } scores;
        int grades;
    };
    
    void changeInfo(struct Student * p);
    void displayInfo(struct Student s);
    
    int main()
    {
        struct Student stuA = {"jerry", 17, {98, 97.5, 96}, 7}; //一次性全部赋值,如果缺少,会有默认值,char *对应null, int, double为0
        displayInfo(stuA);
        changeInfo(&stuA); //使用指针,传递地址
        puts("after changing info: ");
        displayInfo(stuA);
        return 0;
    }
    
    void changeInfo(struct Student * p)
    {
        strcpy(p->name, "George");
        p->age = 45;
        p->grades = 14;
        p->scores.math = 56;
        p->scores.english = 45.5;
        p->scores.science = 56.5;
    }
    
    void displayInfo(struct Student s)
    {
        printf("My name is %s, I'm %d years old and in Grade %d. I got %f %f %f in math, english, science respectively.
    ", s.name, s.age, s.grades, s.scores.math, s.scores.english, s.scores.science);
    }

    返回struct:

    #include <stdio.h>
    #include <string.h>
    struct Student
    {
        char name[10];
        int age;
        struct subjects {
            double math;
            double english;
            double science;
        } scores;
        int grades;
    };
    
    struct Student changeInfo(struct Student * p);
    void displayInfo(struct Student s);
    
    int main()
    {
        struct Student stuA = {"jerry", 17, {98, 97.5, 96}, 7}; //一次性全部赋值,如果缺少,会有默认值,char *对应null, int, double为0
        displayInfo(stuA);
        puts("
    after changing info: ");
        displayInfo(changeInfo(&stuA));
        return 0;
    }
    
    struct Student changeInfo(struct Student * p)
    {
        strcpy(p->name, "George");
        p->age = 45;
        p->grades = 14;
        p->scores.math = 56;
        p->scores.english = 45.5;
        p->scores.science = 56.5;
        return *p;
    }
    
    void displayInfo(struct Student s)
    {
        printf("My name is %s, I'm %d years old and in Grade %d. I got %f %f %f in math, english, science respectively.
    ", s.name, s.age, s.grades, s.scores.math, s.scores.english, s.scores.science);
    }

    注:

    上面的:

    #include <string.h>
    char name[10];
    strcpy(p->name, "George");

    可以换成:

    指针形式,就不需要用到string.h中的strcpy了

    char * name;
    p->name = George
  • 相关阅读:
    vue报错 ModuleBuildError: Module build failed: Error: `sass-loader` requires `node-sass` >=4. Please install a compatible version.
    js 相对路径转为绝对路径
    查询数据库表结构,默认值,是否为空等
    NOLOCK、HOLDLOCK、UPDLOCK、TABLOCK、TABLOCKX
    【58沈剑架构系列】RPC-client异步收发核心细节?
    Windows下性能最好的I/O模型——完成端口
    链表的基本操作
    逐步优化求解最大子序列和
    python 遍历文件夹
    Markdown笔记
  • 原文地址:https://www.cnblogs.com/profesor/p/13109841.html
Copyright © 2011-2022 走看看