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

    #include <stdio.h>

    #include <malloc.h>

    void f(int **q)

    {

        *q=(int *)malloc(sizeof(int));

        **q=5;

    }

    int main(void)

    {

        int *p;

        f(&p);

        printf("%d ",*p);

        return 0;

    }

    p代表的是一个整形变量的地址(指针),因此&p就是指针的指针,f函数的形参用**q表示,*q=(int *)malloc(sizeof(int)); 等价于p=(int

    *)malloc(sizeof(int)); 因为*q就是p,那么**q就是*p

    #include <stdio.h>

    struct student

    {

        int age;

        float score;

        char sex;

    };

    int main(void)

    {

        struct student st={23,66.6f,'M'};

        struct student st1;

        st1.age=22;

        st1.score=88;

        st1.sex='F';

        printf("%d %f %c ",st.age, st.score, st.sex);

        printf("%d %f %c ",st1.age, st1.score, st1.sex);

        return 0;

    }

    上面的程序说明了结构体如何定义,注意结构体定义最后有一个;没有它编译时会报错,在mian函数中说明了结构体的两种赋值方法

    #include <stdio.h>

    struct student

    {

        int age;

        float score;

        char sex;

    };

    int main(void)

    {

        struct student st={23,66.6f,'M'};

        struct student *pst=&st;

        printf("%d %d ",st.age,pst->age);

        return 0;

    }

    因为有了struct student *pst=&st; 这句话,所以pst->age等价于st.age, 输出结果为23 23

     

  • 相关阅读:
    批处理基础知识-IF
    在Windows 10 x64 编译ReactOS-0.4.5源码并在VMare中运行
    复制20天以前指定的文件夹、子文件夹和子文件至指定目录
    bat(批处理)命令(tomcat 7.0.75 startup.bat 命令集)
    mycat
    mysql
    5种网络IO模型
    Linux常用命令
    mybatis多参数传递,延迟加载,缓存,注解开发
    事务,mybatis
  • 原文地址:https://www.cnblogs.com/linuxboke/p/5656374.html
Copyright © 2011-2022 走看看