【问题标题】:Dynamic Array of Structures within a Structure Declaration结构声明中的结构动态数组
【发布时间】:2020-11-09 22:30:06
【问题描述】:

我正在执行以下操作:

struct f
{
//some simple variables here
};

struct p
{
//more simple variables here
struct f fbs[X]; //where x can be between 1-4 and is determined from a variable ran after the code 
                //begins to run
};

struct t
{
struct p pts[4]; //will always contain 4 elements/objects
//finally some more simple variables
};

我想知道如何将 fbs 创建为动态数组,因为 x 的值可以在 1-4 之间变化。我打算将预处理器指令与#if 语句等一起使用,但这不允许在运行时更改变量。

据我所知,malloc 的一些用途是必需的,但到目前为止我还没有找到任何解决方案。

非常感谢任何帮助。

【问题讨论】:

  • 除非 struct f 很大,否则只需声明 fbs[4] 并计算已填满的数量。
  • 大约有 96-288 个这样的结构。将会发生的是这些将由数据库填充。问题是这些结构的大小也将用于图形目的(例如,如果只有 2 个被填充时有 fbs[4],这肯定会导致更大的问题
  • 不,不管你是分配还是使用固定大小的数组,使用存储的方式都是一样的。您要么保留填充元素数量的计数器,要么使用标记元素(例如 NULL 或所有成员为零等,类似于 C 字符串使用 '\0' 的方式)作为下一个数组中最后一个有效元素之后的元素。这是仅有的两种允许您遍历数组中的元素或知道多少的方法。最好使用计数器,因为您不必每次想知道有多少时都进行迭代,但任何一种方式都可以。
  • 仍然很困惑,因为这个结构的构造函数将一次创建 3 个结构 - 最终结构中的最后一个元素是这个 fbs[] 数组。据我所知,在结构中我不能使用 if 等语句,那么我将如何跳过 fbs[] 中的一个元素,以便它不会填充不正确的数据

标签: c struct arduino malloc


【解决方案1】:

我不完全清楚你在最后一条评论中所说的"skip over an element in fbs[] such that it is not filled" 是什么意思,但我认为你可以通过保持所需元素数量的成员计数(比如int n;)来构建struct p,提供在fbs[4] 中填充的数组元素的数量。这样pts[4]fbs[4] 的大小都是固定的,但您只能在fbs[] 中使用n 元素

一个简短的例子将解释。以"some simple variables" 开头的struct f 开头,例如int a, b, c;,例如

#include <iostream>

#define NFBS 4

struct f
{
    int a, b, c;    /* some simple variables */
    
    /* construct f */
    f (int x = 0, int y = 0, int z = 0) : a(x), b(y), c(z) {}
    
    /* overload of << to output variables */
    friend std::ostream& operator<< (std::ostream& os, const f& fs) {
        std::cout << "  " << fs.a << "  " << fs.b << "  " << fs.c << '\n';
        return os;
    }
};

以上只是对abc的直接初始化,每个结构中&lt;&lt;的重载只是提供了一种简单的输出方式和说明用法。

现在对于struct p,将fbs 的声明保持为固定大小fbs[NFBS],但添加一个成员变量int n;,用于保存fbs 中实际使用的元素数量,例如

struct p
{
    int n;                      /* no. of elements (0 <= n < NFBS) */
    struct f fbs[NFBS];         /* array of struct f */
    
    p (int i = 0) : n(i)        /* construct n struct f in fbs array */
    { 
        if (n > NFBS) {         /* validate n */
            std::cerr << "error: struct p initial value out-of-range.\n";
            /* handle error */
        }
        else {
            for (int k = 0; k < n; k++)
                fbs[k] = f(k+1, k+2, k+3);  /* handle as desired */
        }
    }
    
    /* overload of << to output variables */
    friend std::ostream& operator<< (std::ostream& os, const p& ps) {
        std::cout << "have fbs[" << ps.n << "]\n";
        for (int i = 0; i < ps.n; i++)
            std::cout <<  "  fbs[" << i << "]: " << ps.fbs[i];
        return os;
    }
};

上面,构造函数初始化fbsn 元素。 (例如使用的虚拟值)。请求的元素数量根据NFBS 的最大值进行验证——如果它超出范围,您将需要处理该错误,但是您喜欢。

最后对于struct t,你添加相同的计数器成员int n来构造p(n)并初始化pts的每个元素,例如

struct t
{
    int n;                      /* no. of elements for struct p fbs[n] */
    struct p pts[NFBS];         /* array of struct p */
    
    t (int i = 0) : n(i)        /* constuct NFBS struct p, each initialized p(n) */
    {
        for (int k = 0; k < NFBS; k++)
            pts[k] = p(n);      /* handle as desired */
    }
    
    /* overload of << to output variables */
    friend std::ostream& operator<< (std::ostream& os, const t& ts) {
        std::cout << "\nhave pts[" << NFBS << "]\n";
        for (int i = 0; i < NFBS; i++)
            std::cout <<  "\npts[" << i << "]: " << ts.pts[i];
        return os;
    }
};

所以t 总是有pts[4] 每个初始化为struct pfbs 每个0-4 元素之间。对于struct p 具有2 然后在fbs 中使用4 元素的情况,一个简短的main() 实现上述内容将是:

int main () {
    
    t foo{2}, bar{4};
    
    std::cout << foo << bar;
}

使用/输出示例

$ ./bin/multi_struct_arr

have pts[4]

pts[0]: have fbs[2]
  fbs[0]:   1  2  3
  fbs[1]:   2  3  4

pts[1]: have fbs[2]
  fbs[0]:   1  2  3
  fbs[1]:   2  3  4

pts[2]: have fbs[2]
  fbs[0]:   1  2  3
  fbs[1]:   2  3  4

pts[3]: have fbs[2]
  fbs[0]:   1  2  3
  fbs[1]:   2  3  4

have pts[4]

pts[0]: have fbs[4]
  fbs[0]:   1  2  3
  fbs[1]:   2  3  4
  fbs[2]:   3  4  5
  fbs[3]:   4  5  6

pts[1]: have fbs[4]
  fbs[0]:   1  2  3
  fbs[1]:   2  3  4
  fbs[2]:   3  4  5
  fbs[3]:   4  5  6

pts[2]: have fbs[4]
  fbs[0]:   1  2  3
  fbs[1]:   2  3  4
  fbs[2]:   3  4  5
  fbs[3]:   4  5  6

pts[3]: have fbs[4]
  fbs[0]:   1  2  3
  fbs[1]:   2  3  4
  fbs[2]:   3  4  5
  fbs[3]:   4  5  6

不知道您打算如何实现这组嵌套结构的更多信息,很难更精确,但这是我最初的想法,即如何在不同的情况下处理 fbs 中所需的不同数量的元素struct p.

【讨论】:

    猜你喜欢
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    相关资源
    最近更新 更多