1 指针的本质就是地址
*p (p是指针,*p是一个值)
对于指针buffer, buffer[i] 与 *(buffer+i)是等价的,只不过都习惯用p[i]来表示
&buffer[i]与 buffer+i 是等价的,代表地址
uint16_t* 与uint8_t* :前者一次去内存中取两个字节,后者每次去内存中取一个字节
2 回传值需要用指针
example 1
example 2
数组名即为数组的地址,指针本质是地址,其他类型使用地址要用取地址符&
example 3
结构体:传入参数为指针时,使用取地址符&
example 4
输入参数为**
example 5
Json命令返回字符串
example 6 指针函数与函数指针
指针函数:指针函数是指带指针的函数,即本质是一个函数,函数返回类型是某一类型的指针。
用这个函数打印出版本号 char * getVersionCode(); 假设版本号为ABC123
函数指针:指向函数的指针变量
定义 typedef int (*function_pointer_t) ( int arg1, int arg2 );
定义了一个函数指针function_pointer_t,它指向的函数返回值为int类型,输入参数为( int arg1, int arg2 );
.h定义:
typedef struct bes_bt_local_info_t{
uint8_t bt_addr[BTIF_BD_ADDR_SIZE];
const char *bt_name;
uint8_t bt_len;
uint8_t ble_addr[BTIF_BD_ADDR_SIZE];
const char *ble_name;
uint8_t ble_len;
}bes_bt_local_info;
typedef int (*LHDC_GET_BT_INFO)(bes_bt_local_info * bt_info);
//定义了一个函数指针,LHDC_GET_BT_INFO,函数返回值为int型,函数传入参数为一个结构体指针(bes_bt_local_info * bt_info)
bool lhdcSetLicenseKeyTable(uint8_t * licTable, LHDC_GET_BT_INFO pFunc);
//pFunc类似一个回调函数,通过此函数得到bes_bt_local_info结构体里面的信息
.C调用
先定义了pFunc,将BT的相关信息写入
int LHDC_pFunc(bes_bt_local_info * bt_info)
{
uint8_t bt_lhdc_addr[6] = {0x75,0x33,0x33,0x22,0x11,0x11};
uint8_t ble_lhdc_addr[6] = {0x75,0x33,0x33,0x22,0x11,0x11};
for(int i = 0;i < 6;i++)
{
bt_info->bt_addr[i] = bt_lhdc_addr[i];
}
for(int j = 0;j < 6;j++)
{
bt_info->ble_addr[j] = ble_lhdc_addr[j];
}
TRACE("********** %x,%x,%x,%x,%x,%x\n",bt_info->bt_addr[0] ,bt_info->bt_addr[1],bt_info->bt_addr[2],bt_info->bt_addr[3] ,bt_info->bt_addr[4],bt_info->bt_addr[5]);
bt_info->bt_name = "BES2000iZ";
bt_info->bt_len = 9;
bt_info->ble_name = "BES2000iZ";
bt_info->ble_len = 9;
TRACE("********bt_name:%s \n",bt_info->bt_name );
return 0;
}
然后调用:
extern char testkey_bin[];
// testkey_bin[]已在库中集成,在这里用,extern一下
lhdcSetLicenseKeyTable((uint8_t *)testkey_bin,LHDC_pFunc);