双指针两个结构体数组编译错误

发布于 2024-12-14 05:55:12 字数 678 浏览 0 评论 0原文

我定义了这个结构:

typedef struct
{
    uint16_t short_addr;
    uint64_t ieee_addr;
    uint8_t  LQI;
    uint16_t PANId;
} CAMERA;

并且我声明了它的一个数组,如下所示:

static CAMERA device_list [ MAX_NUMBER_OF_DEVICES ];

所有这些都是在特定模块中定义和声明的。 现在,我想从新结构中的主模块创建一个指向该数组的指针。 但我遇到编译错误,而且我不太确定该怎么做。

typedef struct
{
 CAMERA * device_list[ MAX_NUMBER_OF_DEVICES ];
} main_struct;
main_struct MAIN_STRUCT;

但问题是我无法将指针的值分配给它。

MAIN_STRUCT.device_list = device_list;

导致以下编译错误:

Error[Pe137]: expression must be a modifiable lvalue 

正确的方法是什么?

I have this Structure defined:

typedef struct
{
    uint16_t short_addr;
    uint64_t ieee_addr;
    uint8_t  LQI;
    uint16_t PANId;
} CAMERA;

And I declared an array of it, like this:

static CAMERA device_list [ MAX_NUMBER_OF_DEVICES ];

all this is defined and declared in a specific module.
Now, I want to create a pointer to that array from the main module from within a new structure.
but I get compilation errors, and I'm not quite sure how to do that.

typedef struct
{
 CAMERA * device_list[ MAX_NUMBER_OF_DEVICES ];
} main_struct;
main_struct MAIN_STRUCT;

But the problem is that I can't assign the value of the pointer to that.

MAIN_STRUCT.device_list = device_list;

Caused the following compilation error:

Error[Pe137]: expression must be a modifiable lvalue 

What's the right way to do that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

另类 2024-12-21 05:55:12

您不能分配给数组。您还在 main_struct 中声明了它错误,将其声明为指针就足够了(现在您已将其声明为指针数组。)

typedef struct
{
 CAMERA * device_list;
} main_struct;

并且它应该可以工作。

如果您确实希望将其作为数组,则将其声明为数组:

typedef struct
{
 CAMERA device_list[ MAX_NUMBER_OF_DEVICES ];
} main_struct;

并从另一个数组复制到新数组:

memcpy(MAIN_STRUCT.device_list, device_list, sizeof(CAMERA) * MAX_NUMBER_OF_DEVICES);

You can not assign to an array. You also declare it wrong in main_struct, it's enough to declare it as a pointer (right now you have it declared as as array of pointers.)

typedef struct
{
 CAMERA * device_list;
} main_struct;

And it should work.

If you really want it as an array, then declare it as an array:

typedef struct
{
 CAMERA device_list[ MAX_NUMBER_OF_DEVICES ];
} main_struct;

And copy from the other array to the new array:

memcpy(MAIN_STRUCT.device_list, device_list, sizeof(CAMERA) * MAX_NUMBER_OF_DEVICES);
茶花眉 2024-12-21 05:55:12

尝试:

typedef struct
{
 CAMERA * device_list
} main_struct;
main_struct MAIN_STRUCT;



{

   MAIN_STRUCT myMainStruct;
   myMainStruct.device_list = device_list;

}

通过您正在做的事情,您创建了一个指向主结构的指针数组;
马里奥

Try:

typedef struct
{
 CAMERA * device_list
} main_struct;
main_struct MAIN_STRUCT;



{

   MAIN_STRUCT myMainStruct;
   myMainStruct.device_list = device_list;

}

With what you are doing you create an array of pointers to main struct;
hth

Mario

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文