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

 

原文地址:https://www.cnblogs.com/linuxboke/p/5656374.html