zoukankan      html  css  js  c++  java
  • 结构体

    #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;
    加油啦!加油鸭,冲鸭!!!
  • 相关阅读:
    nginx限流方案的实现(三种方式)
    Pthreads并行编程之spin lock与mutex性能对比分析(转)
    C/C++中float和double的存储结构(转)
    list_entry(ptr, type, member)——知道结构体内某一成员变量地址,求结构体地址
    stderr和stdout详细解说(转)
    结构体内存分配理解
    C中的C文件与h文件辨析(转)
    访问vector元素方法的效率比较(转)
    linux c中select使用技巧——计时器(转)
    thread::id
  • 原文地址:https://www.cnblogs.com/clarencezzh/p/5084335.html
Copyright © 2011-2022 走看看