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;
    加油啦!加油鸭,冲鸭!!!
  • 相关阅读:
    Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTraversal
    Linux系统下jar包的启动方式
    1062 Error 'Duplicate entry '1438019' for key 'PRIMARY'' on query
    Linux学习笔记 --iptables防火墙配置
    MySQL学习笔记_10_MySQL高级操作(下)
    MySQL学习笔记_9_MySQL高级操作(上)
    MySQL学习笔记_8_SQL语言基础复习
    Linux 学习笔记_12_文件共享服务_4_SSH
    Linux 学习笔记_12_文件共享服务_3_NFS网络文件服务
    MySQL学习笔记_7_MySQL常用内置函数
  • 原文地址:https://www.cnblogs.com/clarencezzh/p/5084335.html
Copyright © 2011-2022 走看看