简体   繁体   English

如何在 C 中使用 #define 中的指针和定义

[英]How using a pointer and definition from a #define in C

I was wondering exactly how the gattCharCfg_t within this defined snippet is being used.我想知道这个定义片段中的gattCharCfg_t究竟是如何使用的。

Here is the #define snipped:这里是#define 剪断:

    // Client Characteristic Configuration table (from CCC attribute value pointer)
      #define GATT_CCC_TBL( pValue )           ( (gattCharCfg_t *)(*((PTR_TYPE)(pValue))) )

And here is how it is being accessed in a couple spots in the 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 I am not understanding the mechanics of how it is being accessed.我想我不了解它是如何被访问的机制。 I would definitely appreciate a more thorough explanation as I am just diving back into c.我肯定会很感激更彻底的解释,因为我刚刚回到 c。

( (gattCharCfg_t *)(*((PTR_TYPE)(pValue))) )
  1. (PTR_TYPE)(pValue) - cast pValue as a PTR_TYPE (pointer) (PTR_TYPE)(pValue) - 将 pValue 转换为 PTR_TYPE(指针)
  2. *((PTR_TYPE)(pValue)) - look at the memory location pointed to by the pointer *((PTR_TYPE)(pValue)) - 查看指针指向的内存位置
  3. (gattCharCfg_t *)(*((PTR_TYPE)(pValue))) - for a pointer pointing to a gattCharCfg_t structure (could be an array of gattCharCfg_t ) instead of void * (gattCharCfg_t *)(*((PTR_TYPE)(pValue))) - 用于指向gattCharCfg_t结构的指针(可能是gattCharCfg_t的数组)而不是 void *

GATT_CCC_TBL is a macro to shortcut having to write out the conversion. GATT_CCC_TBL是必须写出转换的快捷方式的宏。 With respect to the other code in your post,关于您帖子中的其他代码,

  voltDataConfig = (gattCharCfg_t *)ICall_malloc(sizeof(gattCharCfg_t) *
                                                    linkDBNumConns);
  1. sizeof(gattCharCfg_t) - take the size of gattCharCfg_t , eg, 23 bytes sizeof(gattCharCfg_t) -取的大小gattCharCfg_t ,例如,23个字节

  2. * linkDBNumConns - multiply it by linkDBNumConns, eg, 100 making 2300 bytes * linkDBNumConns - 乘以 linkDBNumConns,例如,100 产生 2300 个字节

  3. ICall_malloc(..) - allocate that much memory ICall_malloc(..) - 分配那么多内存

  4. (gattCharCfg_t *) - cast that location in memory as a pointer to gattCharCfg_t structures (gattCharCfg_t *) - 将内存中的该位置转换为指向gattCharCfg_t结构的指针

Update.更新。 Looks like this code is from the Bluetooth stack, in which case看起来这段代码来自蓝牙堆栈,在这种情况下

typedef struct
{
  uint16 connHandle; //!< Client connection handle
  uint8  value;      //!< Characteristic configuration value for this client
} gattCharCfg_t;

So GATT_CCC_TBL( pValue ) will take a pointer value pValue and turn what it points to to one or more gattCharCfg_t structures - most likely an array of these since _TBL is likely "table".因此GATT_CCC_TBL( pValue )将采用指针值pValue并将其指向一个或多个 gattCharCfg_t 结构 - 最有可能是这些结构的数组,因为 _TBL 可能是“表”。

In the code there is no use of the macro GATT_CCC_TBL 1 anywhere.在代码中,任何地方都没有使用宏GATT_CCC_TBL 1 The code is casting the return value of ICall_malloc() which presumably returns void * 2 to a pointer of type gattCharCfg_t 3 .该代码正在转换ICall_malloc()的返回值,它可能将void * 2返回到gattCharCfg_t 3类型的指针。 If this is really code that is actually not necessary and如果这真的是实际上没有必要的代码并且

voltDataConfig = ICall_malloc(sizeof(gattCharCfg_t) * linkDBNumConns)

and the better version would be更好的版本是

voltDataConfig = ICall_malloc(sizeof(*voltDataConfig) * linkDBNumConns)

would probably be correct.可能是正确的。


1 Which is what #define defines actually. 1这就是#define实际定义的内容。
2 You never need to cast void * to another pointer type in . 2您永远不需要将void *转换为另一种指针类型。
3 Note that gattCharCfg_t is actually a type defined somewhere with a typedef . 3请注意, gattCharCfg_t实际上是在某处用typedef定义的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM