联合体union 枚举enum

时间:2022-09-05 17:50:37

联合体 union

  • 不同类型的变量共同占用一段内存(相互覆盖),联合变量任何时刻只有一个成员存在,节省内存
  • 联合体变量的大小=最大的成员所占的字节数
#include "stdlib.h"
#include "stdio.h"
union Location{
int x;
int y;
double z;
}
void main(){
union Location l1;
l1.x = 100;
l1.y = 80;
l1.z = 18.9;

printf("%d, %d, %f\n", l1.x, l1.y, l1.z);
system("pause");
}
typedef union jvalue{
jboolean z;
jbyte b;
jcahr c;
jshort s;
jint i;
jlong l;
jfloat f;
jdouble d;
jobject l;
} jvalue;

枚举 enum

限定值,保证取值的安全性

//enumeration
enum Day{
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday = 5,
Sunday = 6
}
enum Day{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}

void main(){
//枚举类的值,必须是括号中的值
enum Day d = Monday;
printf("%#x, %d\n", &d, d);
getchar();
}