C语言学习笔记(数组、指针和结构(1))

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值