C/C++之多级指针和结构体

来源:互联网 发布:八爪鱼采集淘宝评论 编辑:程序博客网 时间:2024/06/06 01:25
char *p1  =(char *)malloc(10);//14byte//指针在栈中占4个字节,分配10个字节在堆中,一共14个字节

动态内存和静态内存的比较

静态内存是系统是程序编译执行后系统自动分配,由系统自动释放,静态内存是栈分配的.动态内存是堆分配的.

这里写图片描述

#include<stdio.h>#include<stdlib.h>int main(){    int i = 3;    int *p = &i;    int **q =&p;    int***l = &q;    printf("i的值为%d\n",i);    printf("i的值为%d\n",*p);    printf("i的值为%d\n",**q);    printf("i的值为%d\n",***l);    system("pause");    }

这里写图片描述

#include<stdio.h>#include<stdlib.h>         void study(){              printf("好好学习天天向上\n");              }//定义一个结构体 struct student{       int age;       int height;       char sex;       //结构体内不能定义函数 ,但可以定义函数指针       void(*studyP)();        } main(){    //定义结构体的变量     struct student st = {20,170,'m'};      struct student st1 = {21,171,'w',study};    printf("%d %d %c \n",st.age,st.height,st.sex);    printf("结构体的长度%d\n",sizeof(st));//三个长度是12,如果三个int是12 如果一个char也是12 ,机构体会自动补齐度     // int ,short,char 本来长度是7,但事实是8,把char化成了两个字节,为了方便位移     printf("%d %d %c \n",st1.age,st1.height);    //调用函数指针有三种方法      st1.studyP();   //第二种    struct student* stp = &st1;   (*stp).studyP();   //第三种    stp->studyP();     system("pause");    }

这里写图片描述

这里写图片描述

0 0
原创粉丝点击