如何在C中的数组的任意点初始化多个结构体?

huangapple go评论69阅读模式
英文:

How to initialize multiple structures at an arbitrary point of an array in C?

问题

以下是你提供的代码的翻译部分:

我有一个在C中定义的结构体:

struct problem_spec_point {
        int point_no;
        double x;
        double y;
        int bc;
};

我有一个包含6个这些结构体的数组,前四个结构体在初始化时明确定义了:

static struct point points[6] =
    {
        {0, 1, 2, 5},
        {0, 1, 2, 6},
        {0, 1, 2, 7},
        {0, 1, 2, 8},
    };

我可以稍后添加第5个和第6个结构体:

points[4] = (struct point) {0, 1, 2, 12};
points[5] = (struct point) {3, 3, 3, 3};

但假设我想要一次添加这两个(或多个)连续的结构体,有没有办法可以这样做?

类似于:

points[4] = {(struct point) {0, 1, 2, 12}, (struct point) {3, 3, 3, 3},};

很显然,我尝试了上面列出的语法。

为了背景,实际上我的代码中有一个长度为3n+6的数组。我使用一个for循环为前面的3n个结构体分配它们的值,但最后的6个结构体具有不同的奇怪模式。

for(int i = 0; i < 3n; i++)
{
 point[i] = stuff;
}

//数组末尾的奇怪结构体

我可以交换顺序并进行一些索引更改:

static struct point points[3n+6] = 
{
  // 先放最后的结构体
}

for(int i = 6; i < 3n+6, i++)
{
  point[i] = stuff;
}

但我不想这样做。我对C不是特别熟悉,我很好奇。

这是我的第一篇帖子,如果我没有遵循正常格式,请原谅我。

英文:

I have a structure in C defined by


struct problem_spec_point {
        int point_no;
        double x;
        double y;
        int bc;
};

I have an array of these structures of length 6, with the first four structures defined explicitly on initialization.

static struct point points[6] =
    {
        {0, 1, 2, 5},
        {0, 1, 2, 6},
        {0, 1, 2, 7},
        {0, 1, 2, 8},
    };

I can add the 5th and 6th structures later:
points[4] = (struct point) {0,1,2,12};
points[5] = (struct point) {3,3,3,3};

But suppose that I wanted to add these two (or multiple) consecutive structures, all at once.
Something like

points[4] = {(struct point) {0,1,2,12}, (struct point) {3,3,3,3},};

Is there any such way to do this?

Obviously I tried the syntax listed above.

For context, in reality my code has an array of length 3n+6. I have a for-loop assigning the first 3n structures their values, but the last 6 have a different, odd pattern.

for(int i = 0; i < 3n; i++)
{
 point[i] = stuff;
}

//odd points here at end of array

I could switch the order and do some indexing changes:

static struct point points[3n+6] = 
{
  last points first
}

for(int i = 6; i < 3n+6, i++
{
  point[i]=stuff;
}

But I don't want to. I am not super familiar with C, and I am curious.

This is my first post, so please forgive me if I did not follow the normal format.

答案1

得分: 3

以下是翻译好的部分:

最好的方式可能只是在可读的代码中键入它:

points[4] = (struct point) {0,1,2,12};
points[5] = (struct point) {3,3,3,3};

或者对于具有相同值的大间隔,可以使用memcpy。也许可以考虑以下方式:

#include <string.h>;

void add_points (struct point* points, const struct point* val, size_t from, size_t to)
{
  for(size_t i=from; i<=to; i++)
  {
    memcpy(&points[i], val, sizeof(struct points));
  }
}

// 用法示例:从索引4到索引5分配{3,3,3,3}

add_points(points, &(struct point){3,3,3,3}, 4, 5);

(此代码可以通过使用restrict指针稍微优化。)

英文:

The best way is likely just to type it out in plain, readable code:

points[4] = (struct point) {0,1,2,12};
points[5] = (struct point) {3,3,3,3};

Alternatively for big intervals with the same value etc, use memcpy. Perhaps something along the lines of this:

#include &lt;string.h&gt;

void add_points (struct point* points, const struct point* val, size_t from, size_t to)
{
  for(size_t i=from; i&lt;=to; i++)
  {
    memcpy(&amp;points[i], val, sizeof(struct points));
  }
}


// usage example: assign {3,3,3,3} from index 4 to index 5

add_points(points, &amp;(struct point){3,3,3,3}, 4, 5);

(This code can be optimized slightly by using restrict pointers.)

答案2

得分: 0

Lundin提到您可以使用memcpy。他没有明确提到您可以通过memcpy重新构建不同值的尝试:

	memcpy(points + 4, (struct point []) { {0,1,2,12}, {3,3,3,3} },
	       sizeof(struct point [2]));
英文:

Lundin rightly told that you can use memcpy. He didn't explicitly mention that you can reformulate your attempt for different values

points[4] = {(struct point) {0,1,2,12}, (struct point) {3,3,3,3},};

with memcpy like

	memcpy(points+4, (struct point []){ {0,1,2,12}, {3,3,3,3} },
			  sizeof (struct point [2]));


</details>



huangapple
  • 本文由 发表于 2023年4月11日 14:24:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定