#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(int argc, const char * argv[]) {
void setStudent();
setStudent();
return 0;
}
#pragma - Struct
typedef struct {//sizeof(Student)==16
int id;//4自动补齐4
char *name;//8
}Student;
typedef struct {//sizeof(Student1)==8
int id;//4
char name;//1自动补齐3
}Student1;
void setStudent(){
Student st1;
st1.id = 2;
st1.name = (char *)malloc(sizeof(char));
strcpy(st1.name,"Yanbao");
//Student *st = &st1;
Student *st = (Student*)malloc(sizeof(Student));
st->id = 1;
st->name = "Harry";
printf("%zi,%zi,%zi,%zi\n",sizeof(st),sizeof(Student),sizeof(char),sizeof(int));
//output:8,16,1,4
printf("%d,%s\n",st->id,st->name);//st->name取出堆中的值,output:1,Harry
}
结构体内存大小
最新推荐文章于 2024-02-28 18:53:55 发布