• 结构体


    #include<stdio.h>
    #include<string.h>
    struct Student{
        int sid;
        char name[200];
        int age;
    };                        //分号不能省略
    void main(){
        struct Student st={1000,"zhangsan",20};
        printf("%d % s %d
    ",st.sid,st.name,st.age);
        st.age=99;
        //st.name="lisi";
        strcpy(st.name,"lisi");
        st.sid=10000;
        printf("%d % s %d
    ",st.sid,st.name,st.age);
        //printf("%d % s %d
    ",st); 

        struct Student * pst;
        pst=&st;
        pst->sid=99;    //等价于(*pst).sid

    }

    image

    #include<stdio.h>
    #include<string.h>
    struct Student{
        int sid;
        char name[200];
        int age;
    };
    void f(struct Student * pst){
        (*pst).sid=99;
        pst->age=10;
        strcpy(pst->name,"zhangsan");
    }
    
    void g(struct Student st){
        printf("%d %s %d
    ",st.sid,st.name,st.age);
    }
    void g1(struct Student * pst){
        printf("%d %s %d
    ",pst->sid,pst->name,pst->age);    
    }
    void main(){
        struct Student st;
        f(&st);
        //printf("%d %s %d
    ",st.sid,st.name,st.age);
        g(st);
        g1(&st);
    } 

    image

    malloc函数的使用

    #include<stdio.h>
    #include<malloc.h>
    void main(){
        int a[]={4,10,2,8,6};
    
        int len;
        printf("请输入你要的长度:");
        scanf("%d",&len);
        int * parr=(int *)malloc(sizeof(int)*len);
    /*    *parr=4;
        parr[1]=10;
        printf("%d %d",*parr,parr[1]);
    */
        for(int i=0;i<len;i++){
            scanf("%d",&parr[i]);
        }
    
        for( i=0;i<len;i++){
            printf("%d 
    ",*(parr+i));
        }
    
        free(parr);
    }

    跨内纯使用函数:

    #include<stdio.h>
    #include<malloc.h>
    struct Student{
        int sid;
        int age;
    };
    struct Student * creatStudent(){
        struct Student * pst=(struct Student *)malloc(sizeof(struct Student));
        pst->age=10;
        pst->sid=110;
        return pst;
    }
    void showStudent(struct Student * pst){
        printf("%d  %d
    ",pst->age,pst->sid);
    }
    void main(){
        struct Student st;
        struct Student * pst;
        pst=creatStudent();
        showStudent(pst);
    }

    typedet的使用方法:

    typedef int INT;//位已用的数据类型起一个名字
    typedef struct Student{
        int sid;
        char name[200];
        int age;
    }ST;            //struct Student st等价于ST st
    
    typedef struct Student{
        int sid;
        char name[200];
        int age;
    } ST, * PST;//struct Student *   pst等价于 PST pst;
    加油啦!加油鸭,冲鸭!!!
  • 相关阅读:
    NetBeans IDE 配置XDebug
    XAMPP环境下用phpStorm+XDebug进行断点调试的配置
    vim最通用编辑器命令
    MySQL全文索引应用简明教程
    完美解决 nginx No input file specified.
    htmlspecialchars()和htmlspecialchars_decode()
    中文CentOS 6.5网络配置及设置IP地址
    简单楼层导航代码
    linux重启nginx后出现nginx的pid丢失的解决方案
    nginx启动、关闭、重启
  • 原文地址:https://www.cnblogs.com/clarencezzh/p/5084335.html
Copyright © 2020-2023  润新知