今天看CSR ADK2.5.1一段代码, 获取一组pskey数据, 使用了有点点绕的指针数据结构, 来分析下:
- 先上获取一个数据的通用表达式. 分析的目的就是如何获取这个表达式
key_type * key;
key = *((key_type**)default_configs[config_id] + key_id);
- default_configs的定义:
/* Table of default configurations */
const config_type* const default_configs[] =
{
/* CSR Sink Stereo */
&csr_stereo_default_config,
/* CSR Sink Mono */
&csr_mono_default_config,
/* CSR Car Kit */
&csr_car_default_config,
0,
...
0
};
- config_type的定义:
typedef struct
{
const key_type* battery_config; /* PSKEY_USR_0 - Battery configuration */
const key_type* button_config; /* PSKEY_USR_1 - Button configuration */
const key_type* button_pattern_config; /* PSKEY_USR_2 - Button Sequence Patterns*/
const key_type* supported_features_config; /* PSKEY_USR_3 - HFP supported features block */
...
const key_type* soundbar_hid_remote_conf; /* PSKEY_USR_33 - soundbar HID remote control */
} config_type;
- csr_stereo_default_config的定义:
/* Default Configuration */
const config_type csr_stereo_default_config =
{
(key_type *)&battery_config, /* PSKEY_USR_0 - Battery configuration */
(key_type *)&button_config, /* PSKEY_USR_1 - Button configuration */
(key_type *)&button_pattern_config, /* PSKEY_USR_2 - */
(key_type *)&at_commands, /* PSKEY_USR_3 - AT Commands*/
(key_type *)&pio_block, /* PSKEY_USR_4 - Input/output pio block*/
(key_type *)&HFP_features_config, /* PSKEY_USR_5 - The HFP Supported features*/
(key_type *)&timeouts, /* PSKEY_USR_6 - Timeouts*/
(key_type *)&tri_col_leds, /* PSKEY_USR_7 - Tri Colour LEDS */
(key_type *)&deviceId_Info, /* PSKEY_USR_8 - Device Id information*/
(key_type *)&lengths, /* PSKEY_USR_9 - length of tts, led filter, led states, led events, tones*/
...
(key_type *)&soundbar_hid_remote_conf /* PSKEY_USR_33 - Soundbar HID remote configuration */
}
- 如开篇问题, 为何第一个数据获取方法是key =
*((key_type**)default_configs[0] )
;
分析:
(1) default_configs[0]即&csr_stereo_default_config,它是一個config_type指針,即一個config_type結構的首地址,
這個地址同時也是其第一個元素battery_config的地址,,也就是説如果對它進行*運算,得到的當然就是battery_config的内容了。
battery_config的内容是什麽呢?就是一個key_type*
,其地址當然是key_type**
,所以爲了類型正確,先做一個key_type **
轉換。
(2) battery_config的地址可表示为
(key_type**)(default_configs[0])
对这个数据取值, 就是*((key_type**)default_configs[config_id])
;
对某个pskey取值, 当然是*((key_type**)default_configs[config_id] + key_id)
;