• 结构体


    #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;
    加油啦!加油鸭,冲鸭!!!
  • 相关阅读:
    HTML学习笔记
    JSP与Servlet的跳转及得到路径方法整理(转)
    Servlet 学习笔记6:Cookie
    工作=娱乐=爱[龙]
    幸福的方法[龙]
    10张海报,激励人生[龙]
    8个方法让你安然度过低效率的日子[龙]
    使用空余时间的20个有效途径
    人生三点钟
    2013计划
  • 原文地址:https://www.cnblogs.com/clarencezzh/p/5084335.html
Copyright © 2020-2023  润新知