How using a pointer and definition from a #define in C -


i wondering how gattcharcfg_t within defined snippet being used.

here #define snipped:

    // client characteristic configuration table (from ccc attribute value pointer)       #define gatt_ccc_tbl( pvalue )           ( (gattcharcfg_t *)(*((ptr_type)(pvalue))) ) 

and here how being accessed in couple spots in given code:

    // characteristic configuration: voltage     static gattcharcfg_t *voltdataconfig;        // allocate client characteristic configuration table   voltdataconfig = (gattcharcfg_t *)icall_malloc(sizeof(gattcharcfg_t) *                                                     linkdbnumconns);     if (voltdataconfig == null)     {       return (blememallocerror);     } 

i guess not understanding mechanics of how being accessed. appreciate more thorough explanation diving c.

( (gattcharcfg_t *)(*((ptr_type)(pvalue))) ) 
  1. (ptr_type)(pvalue) - cast pvalue ptr_type (pointer)
  2. *((ptr_type)(pvalue)) - @ memory location pointed pointer
  3. (gattcharcfg_t *)(*((ptr_type)(pvalue))) - pointer pointing gattcharcfg_t structure (could array of gattcharcfg_t) instead of void *

gatt_ccc_tbl macro shortcut having write out conversion. respect other code in post,

  voltdataconfig = (gattcharcfg_t *)icall_malloc(sizeof(gattcharcfg_t) *                                                     linkdbnumconns); 
  1. sizeof(gattcharcfg_t) - take size of gattcharcfg_t, e.g., 23 bytes

  2. * linkdbnumconns - multiply linkdbnumconns, e.g., 100 making 2300 bytes

  3. icall_malloc(..) - allocate memory

  4. (gattcharcfg_t *) - cast location in memory pointer gattcharcfg_t structures

update. looks code bluetooth stack, in case

typedef struct {   uint16 connhandle; //!< client connection handle   uint8  value;      //!< characteristic configuration value client } gattcharcfg_t; 

so gatt_ccc_tbl( pvalue ) take pointer value pvalue , turn points to 1 or more gattcharcfg_t structures - array of these since _tbl "table".


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -