我的指向结构的指针数组有什么问题?

What's wrong with my array of pointers to arrays of pointers to structs?

本文关键字:什么 问题 数组 指针 结构 我的      更新时间:2023-10-16

一个简短的解释,我想做什么:我想构建一个由structs表示的数据元素的层次树。这些元素应该是双重链接的,这样我就可以走路了。我不想使用动态分配,因为树的结构在运行时不会改变。

struct menu {
  uint8_t type;  // typ des Menüpunkts
  struct menu * parent; // Pointer auf den übergeordneten MP
  struct menu *(*children)[]; // Pointer auf untergeordnete MP
  };  
struct menu *(*menll[5])[]; // auxillary array
struct menu gl_menlist[5]=
{
  {0,0,menll[0]}, 
  {0,0,menll[1]},
  {0,0,menll[2]},
  {0,0,menll[3]},
  {0,0,menll[4]}
};
struct menu * rxakvt01[]= {&gl_menlist[3], &gl_menlist[4]}; 
menll[0]=&rxakvt01;  

代码在最后一行失败,并显示以下错误消息:

In file included from Dis0_10.ino:6: var/folders/jl/nv1qvh6n569cxq9xxfd6dx980000gn/T/build753942546562757431.tmp/initialisation.h:71: error: expected constructor, destructor, or type conversion before '=' token

在将变量和数组的初始化移动到函数代码后,我有一个新的错误消息;更有意义的是:

/[path_truncated]/initialisation.cpp: In function 'void shit()':
/[path_truncated]/initialisation.cpp:46: error: cannot convert 'menu* (*)[2]' to 'menu* (*)[]' in assignment

menu *(*)[2]转换为menu *(*)[]的失败很可能是编译器的缺陷。

如果子菜单的大小不需要是动态的,那么只需声明

struct menu *(*children)[2];

struct menu *(*menll[5])[2];

你应该没事的。

如果你需要动态菜单大小,你需要知道子菜单在某个时候的长度,而这是编译器不会为你推导的,所以建议用某种哨兵来指示子菜单的结束。

我发现了另一件可能不是你想要的东西。gl_menlist的定义包含定义时menll的值,以后的赋值不会改变这一点。以下是应该起作用的:

struct menu {
    uint8_t type;  // typ des Menüpunkts
    struct menu * parent; // Pointer auf den übergeordneten MP
    struct menu ***children; // Pointer auf untergeordnete MP
};
struct menu *(*menll[5]);
struct menu gl_menlist[5] =
{
    { 0, 0, &menll[0] },
    { 0, 0, &menll[1] },
    { 0, 0, &menll[2] },
    { 0, 0, &menll[3] },
    { 0, 0, &menll[4] }
};
struct menu *rxakvt01[] = { &gl_menlist[2], 0 };
struct menu *rxakvt02[] = { &gl_menlist[3], &gl_menlist[4], 0 };
menll[0] = rxakvt01;
menll[1] = rxakvt02;