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));
}