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");
}