码迷,mamicode.com
首页 > 编程语言 > 详细

c语言学习的第13天2

时间:2016-07-09 19:34:42      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

#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\n",*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\n",st.age, st.score, st.sex);

    printf("%d %f %c\n",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\n",st.age,pst->age);

    return 0;

}

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

 

c语言学习的第13天2

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!