VC中的指针的强大不需要我多做介绍了,而且使用起来也非常的方便,但是使用得不好的,也会造成灾难。
这篇文章我会记下自己在指针使用过程中的心得跟体会。
①指向先来看一个简单的例子
#define COUNTOF(array) (sizeof(array) / sizeof(*array))
int main(void){
INT32 item_index[10];
memset(item_index, -1, sizeof(item_index));
for(icount=0;icount<COUNTOF(item_index);icount++){
printf("[%d]",item_index[icount]);
}
printf("\n");
printf("sizeof(item_index)=[%d]\n",sizeof(item_index));
printf("sizeof(*item_index)=[%d]\n",sizeof(*item_index)); //指针指向数组的首个元素
printf("[%d]\n",*item_index); //指针指向数组的首个元素
printf("[%p]\n",item_index); //数组首个元素地址
}
结果:[-1][-1][-1][-1][-1][-1][-1][-1][-1][-1]
40
4
-1
0012FE78
②指针的属性
#include <stdio.h>
int MAX(unsigned long, unsigned long, unsigned long);
#define DESIGN_FAX_LEVEL1_OPT_DW_FPROP 1
#define RGB88(g,b) ((g)>(b)?(g):(b))
#define DESIGN_FAX_LEVEL1_OPT_DW_STR_COLOR RGB88(20,30)
#define DESIGN_FAX_LEVEL1_OPT_DW_EDGE_STR_COLOR RGB88(40,10)
#define DESIGN_FAX_LEVEL1_OPT_DW_G_STR_COLOR RGB88(200,300)
#define DESIGN_FAX_LEVEL1_OPT_DW_G_EDGE_STR_COLOR RGB88(10,40)
typedef unsigned int UINT32;
typedef struct{
unsigned long flag;
unsigned long fontMainCoolor;
unsigned long fontSubColor;
} SCRNRC_FONT_PROP_T;
typedef struct {
SCRNRC_FONT_PROP_T off;
SCRNRC_FONT_PROP_T on;
SCRNRC_FONT_PROP_T select_off;
SCRNRC_FONT_PROP_T select_on;
SCRNRC_FONT_PROP_T disable;
} SCRNRC_FONT_PROPSET_T;
SCRNRC_FONT_PROPSET_T scrnrc_fontprop_faxLv1_opt_p = {
{DESIGN_FAX_LEVEL1_OPT_DW_FPROP, DESIGN_FAX_LEVEL1_OPT_DW_STR_COLOR, DESIGN_FAX_LEVEL1_OPT_DW_EDGE_STR_COLOR },
{DESIGN_FAX_LEVEL1_OPT_DW_FPROP, DESIGN_FAX_LEVEL1_OPT_DW_STR_COLOR, DESIGN_FAX_LEVEL1_OPT_DW_EDGE_STR_COLOR },
{DESIGN_FAX_LEVEL1_OPT_DW_FPROP, DESIGN_FAX_LEVEL1_OPT_DW_STR_COLOR, DESIGN_FAX_LEVEL1_OPT_DW_EDGE_STR_COLOR },
{DESIGN_FAX_LEVEL1_OPT_DW_FPROP, DESIGN_FAX_LEVEL1_OPT_DW_STR_COLOR, DESIGN_FAX_LEVEL1_OPT_DW_EDGE_STR_COLOR },
{DESIGN_FAX_LEVEL1_OPT_DW_FPROP, DESIGN_FAX_LEVEL1_OPT_DW_G_STR_COLOR, DESIGN_FAX_LEVEL1_OPT_DW_G_EDGE_STR_COLOR }
};
int main(){
unsigned long r = 100,
g = 200,
b = 300;
int icount = 0,
jcount = 0;
unsigned long * p;
SCRNRC_FONT_PROP_T *str = &scrnrc_fontprop_faxLv1_opt_p.off;
for(icount=0; icount < sizeof(SCRNRC_FONT_PROPSET_T)/sizeof(SCRNRC_FONT_PROP_T); icount++){
p = &((*str).flag);
for(jcount = 0; jcount < sizeof(SCRNRC_FONT_PROP_T)/sizeof(unsigned long); jcount++){
printf("[%d]",* p);
p++; //地址加上一个sizeof(unsigned long)的长度
}
printf("\n");
str++; //地址加上一个sizeof(SCRNRC_FONT_PROP_T)的长度。
}
}