英文:
Use static variable in array declaration
问题
以下是您提供的代码的翻译:
// 注意:这会将onoff_pub_0隐式定义为静态变量
ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_0, 2 + 3, ROLE_NODE);
static esp_ble_mesh_gen_onoff_srv_t onoff_server_0 = {
.rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP,
.rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP,
};
// ...
static esp_ble_mesh_model_t root_models[] = {
ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server_0),\
// ...
};
我明白您只需要翻译代码部分,不需要其他内容。
英文:
I have the following code:
// note: this implicity defines onoff_pub_0 as a static
ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_0, 2 + 3, ROLE_NODE);
static esp_ble_mesh_gen_onoff_srv_t onoff_server_0 = {
.rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP,
.rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP,
};
// ...
static esp_ble_mesh_model_t root_models[] = {
ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server_0),\
// ...
};
I'd like to be able to isolate these definition into separate file, and root_models
in a separate file (to allow for reusability of code), but I can't seem to get around accessing static variables from another file (and yes, I've read a lot of posts on StackOverflow about using statics in C for encapsulation)... Is there any better option when the library I'm using implements static variables in their macros?
FYI, here's a more complete example if someone wants to propose a more general architecture approach...
答案1
得分: 3
如果 onoff_pub_0
由于你所使用的库而不可避免地被声明为 static
,而你希望在不同的文件中访问该变量,那么你可以创建一个函数来返回指向 onoff_pub_0
的指针(只要你知道 onoff_pub_0
的 C 类型是什么)。
所以在 file1.c 中,你可以这样做(我在这里使用 onoff_pub_t
作为类型):
ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_0, 2 + 3, ROLE_NODE);
static esp_ble_mesh_gen_onoff_srv_t onoff_server_0 = {
.rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP,
.rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP,
};
static esp_ble_mesh_model_t root_models[] = {
ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server_0),\
// ...
};
// 函数返回指向 onoff_pub_0 的指针
onoff_pub_t GetOnOff0PubPtr(void)
{
return &onoff_pub_0;
}
然后在 file1.h 中:
#include "some header file .h" // 包含必要的头文件以声明 onoff_pub_t
onoff_pub_t GetOnOff0PubPtr(void);
现在在 file2.c 中:
#include "file1.h"
onoff_pub_t *on_off_pub_0_ptr = GetOnOff0PubPtr(); // 获取指向 file2.c 中静态 on_off_pub_0 的指针
if (*on_off_pub_0_ptr == ...) // 通过解引用 on_off_pub_0_ptr 来操作 on_off_pub_0 的值
英文:
If onoff_pub_0
is unavoidably static
(due to to the library you're using), and you want to access that variable in a different file to where it's defined, then you can create a function to return a pointer to onoff_pub_0
(as long as you know what C type onoff_pub_0
is).
So in file1.c you have (where I'm using onoff_pub_t
for the type):
ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_0, 2 + 3, ROLE_NODE);
static esp_ble_mesh_gen_onoff_srv_t onoff_server_0 = {
.rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP,
.rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP,
};
static esp_ble_mesh_model_t root_models[] = {
ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server_0),\
// ...
};
// Function to return a pointer to onoff_pub_0
onoff_pub_t GetOnOff0PubPtr(void)
{
return &onoff_pub_0;
}
Then in file1.h you have:
#include "some header file .h" // include whatever is necessary for onoff_pub_t
onoff_pub_t GetOnOff0PubPtr(void);
And now in file2.c:
#include "file1.h"
onoff_pub_t *on_off_pub_0_ptr = GetOnOff0PubPtr(); // get a pointer to static on_off_pub_0 in file2.c
if (*on_off_pub_0_ptr == ...) // do something with the value of on_off_pub_0 by dereferencing on_off_pub_0_ptr
答案2
得分: 1
static
在文件范围内的整个目的是限制这些声明只在该单一翻译单元中可见。
如果您想使用来自其他翻译单元的定义,它们需要是extern
,如果您不指定static
,则默认为这个级别。
英文:
The whole purpose of static
at file scope is to restrict visibility of those declarations to that single translation unit.
If you want to use definitions from other TUs, they need to be extern
, which is the default at this level if you don't specify static
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论