c - 用前一个 C 初始化结构变量

标签 c arrays struct

<分区>

我想要一个像下面这样的结构:

typedef struct Mystruct
{
    double  p;
    double  x_min;
    double  x_max;
    double y_min;
    double y_max;
    double params[] = {x_min, x_max, y_min, y_max};
 };

我的目标是数组 params 的元素应该由 { x_min, x_max, y_min, y_max} 填充。

有没有办法在中实现这个结构? ?

最佳答案

在 C 中你不能这样做,相反你可以编写一个函数来为结构实例进行初始化。

typedef struct Mystruct
{
        double  p;
        double  x_min;
        double  x_max;
        double y_min;
        double y_max;
        double params[4];
}Mystruct;


Mystruct GetMystruct( double p, 
                      double x_min, 
                      double x_max, 
                      double y_min, 
                      double y_max)
{
    Mystruct my = { p, x_min,x_max,y_min,y_max, {x_min,x_max,y_min,y_max} } ;

    return my;
}

然后,例子:

Mystruct m = GetMystruct( 0.0, 42.1, 42.2, 42.3,42.4 );

关于c - 用前一个 C 初始化结构变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48381387/

相关文章:

c - git notes 迭代的例子

linux - 2 关于linux内核内存检查点的问题(自定义实现)

c - c中的矩阵位置访问

arrays - 检查连续数字

pointers - 取消引用结构会返回结构的新副本吗?

struct - 如何在方案中实现Racket风格的结构?

python - 使用 Swig、python 包装 Patricia Tries 的问题

C++ glDrawElements 数组作为参数 EXC_BAD_ACCESS 错误

arrays - 我在对象数组上使用 ngFor,而在 html 中数据未显示

c - 想要从C中的结构体内存中访问结构体指针类型的数据