1、 使用联合节省内存资源
a、不使用联合
struct EmpDates
{
int days_works;
struct LastDate
{
intmonth;
intday;
intyear;
}last_day;
};
存储结构如下表示:
int days_works |
int month |
int day |
int year |
b、使用联合
union EmpDates
{
int days_works;
struct LastDate
{
intmonth;
intday;
intyear;
}last_day;
};
使用联合之后的存储结构:
int month or int days_works |
int day |
int year |
2、 使用REGS—一种典型的联合
struct WOEDREGS
{
unsignedint ax, bx,cx,dx,si,di,cflag,flags;
};
struct BYTEREGS
{
unsignedchar al,ah,bl,bh,cl,ch,dl,dh;
};
union REGS
{
struct WOEDREGS x;
struct BYTEREGS h;
};
具体使用:
union REGS intregs;
intregs.h.ah = 0x30;
intregs.h.ah = 0;
printf("%d.%d/n",intregs.h.ah,intregs.h.al);
当程序访问某个通用寄存器(AXBX CX DX),PC允许以16位(字)的格式引用寄存器。但是,用户也可以用寄存器的高字节和低字节应用寄存器(AL,AH,BL,BH,CL,CH,DL,DH)。因为这两种方法都可以应用同一个寄存器,所以可以使用两种方法访问同一存储地址,从而进一步节省个内存的空间。
3、 在数组中查找指定的值
#define ARRY_SIZE 8
int found = 0;
int i = 0;
int arry[ARRY_SIZE] = {1,2,3,4,5,6,7,8};
while((i < ARRY_SIZE) && (!found))
if(arry[i] == 7)
found= true;
else
i++;
if(i < ARRY_SIZE)
printf("the number is found/n");
else
printf("the number is not found!/n");