zoukankan      html  css  js  c++  java
  • 学习c语言的第14天

    #include <stdio.h>

    #include <string.h>

    struct student

    {

        int age;

        char sex;

        char name[100];

    };

    void input(struct student *);

    void output(struct student ss);

    int main(void)

    {

        struct student st;

        input(&st);

        output(st);

        return 0;

    }

    void output(struct student ss)

    {

        printf("%d %c %s ",ss.age, ss.sex, ss.name);

    }

    void input(struct student *pstu)

    {

        (*pstu).age=10;

        strcpy(pstu->name, "张三");

        pstu->sex='M';

    }

    input(&st); 对结构体变量输入,必须发送st的地址

    output(st); 对结构体变量输出,可以发送st的地址也可以直接发送st的内容

    这个程序的功能是通过函数完成对结构体的输入输出

    #include <stdio.h>

    #include <string.h>

    struct student

    {

        int age;

        char sex;

        char name[100];

    };

    void input(struct student *);

    void output(struct student *);

    int main(void)

    {

        struct student st;

        printf("%d ",sizeof(st));

        input(&st);

        output(&st);

        return 0;

    }

    void output(struct student *pst)

    {

        printf("%d %c %s ",pst->age, pst->sex, pst->name);

    }

    void input(struct student *pstu)

    {

        (*pstu).age=10;

        strcpy(pstu->name, "张三");

        pstu->sex='M';

    }

    上面这个程序体现了指针的优点,printf("%d ",sizeof(st)); st占用了108个字节,output(&st); &st是个地址变量只占8个字节,如果是发送

    内容像前面一个程序写成oupt(st); 则此时st占用了108个字节,这样就浪费了内存空间,也使传输速度变慢,因为output(st); 中的st传递

    void output(struct student ss)需要传108个字节,而改为指针的话只需8个字节

    #include<stdio.h>

    enum weekday

    {

        monday, tuesday, wednesday, thursday, friday, saturday, sunday

    };

    int main(void)

    {

        enum weekday day=monday;

        printf("%d ",day);

        return 0;

    }

    这是一个枚举例子,输出的是0,如果是tuesday则是1,依次类推enum weekday 是一个数据类型,前面的数据结构体struct student也可以

    说是一个数据类型

  • 相关阅读:
    进程(第三部分)
    02_jni_hello_c函数介绍
    01_ndk目录介绍
    00_前情回顾
    06_锅炉压力案例_progressbar实现
    05_锅炉压力案例_java实现
    ASP.NET MVC的过滤器笔记
    ASP.NET MVC的过滤器笔记
    ASP.NET MVC的过滤器笔记
    ASP.NET MVC的过滤器笔记
  • 原文地址:https://www.cnblogs.com/linuxboke/p/5657738.html
Copyright © 2011-2022 走看看