2

I'd like to fill array of MyStruct with same value. How can it be done in fastest and simplest way? I'm operating on rather low-level methods, like memset or memcpy.

edit: std::fill_n indeed complies and works fine. But it's C++ way. How can it be done in pure C?

struct MyStruct
{
    int a;
    int b;
};

void foo()
{
    MyStruct abc;
    abc.a = 123;
    abc.b = 321;

    MyStruct arr[100];
    // fill 100 MyStruct's with copy of abc
    std::fill_n(arr, 100, abc); // working C++ way

    // or maybe loop of memcpy? But is it efficient?
    for (int i = 0; i < 100; i++)
        memcpy(arr[i],abc,sizeof(MyStruct));
}
4

6 回答 6

8

请注意正确键入类型名称(区分大小写),并且不要忘记定义后的分号 your struct,除此之外,您的程序应该可以毫无问题地编译:

#include <iostream>
#include <algorithm>

struct MyStruct
{
    int a;
    int b;
}; // <------------- HERE

int main() {
    MyStruct abc;
    abc.a = 123;
    abc.b = 321;

    MyStruct arr[100];
    std::fill_n(arr, 100, abc);

    std::cout << arr[99].b;
}

输出321


“如何以最快最简单的方式完成?”

最简单的方法可能是使用std::vector及其适当的构造函数:

#include <vector>

void foo()
{
    MyStruct abc;
    abc.a = 123;
    abc.b = 321;

    std::vector<MyStruct> vec(100, abc);
    ...
}
于 2013-10-20T18:40:38.473 回答
2
for(int i = 0; i < 100; i++)
{
    arr[i] = abc;
}

最快最干净。优化器很可能也会发挥它的魔力。

于 2013-10-20T18:54:39.343 回答
1

以下应该在C中工作

MyStruct arr[100] = {
   {123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},
   {123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},
   {123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},
   {123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},
   {123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},
   {123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},
   {123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},
   {123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},
   {123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},
   {123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321},{123,321}};
于 2013-10-20T19:46:04.600 回答
0

我在 C99 中的猜测如下代码:

#include <stdio.h>

typedef struct MyStruct
{
    int a ;
    int b ;
} MyStruct_t;

const MyStruct_t abc = 
{
    .a = 0,
    .b = 321, 
};

void main(void)
{
    int i = 0 ;

    MyStruct_t arr[100] = {0} ;

    for(i=0 ;i <sizeof(arr)/sizeof(arr[0]);i++)
    {
        arr[i] = abc ;
    }
}

在我看来,这是最干净、最安全的解决方案。

于 2013-10-21T07:22:37.950 回答
0

不是纯 C,但 GCC 可以让你做得很好:

MyStruct arr[100] = { [ 0 ... 99 ] = { .a = 123, .b = 321 } };

如果您需要纯 C,我会遵循 Tri-Edge Al 的回答

于 2013-10-20T21:31:50.603 回答
0

这是我在c中的做法:

#include <stdio.h>

struct MyStruct{
    int a;
    int b;
};

/*fill the array with a specified element.*/
void fillArray(void* array, int arraySize, void* elem, int elemSize){
    for(int i = 0; i < arraySize; i++){
        memcpy((char*)array + i * elemSize, elem, elemSize);
    }
}

int main(){
    struct MyStruct array[10];
    struct MyStruct elem;
    elem.a = 123;
    elem.b = 321;
    fillArray(array, 10, &elem, sizeof(struct MyStruct));
    for(int i = 0; i < 10; i++){
        printf("%i, %i\n", array[i].a, array[i].b);
    }
    return 0;
}

我创建了一个名为的函数fillArray,用于memcpy将指定的元素复制到数组的每个槽中。我现在只是将相应的元素放入函数参数中并打印出来。

于 2022-02-27T00:01:01.487 回答