Reputation: 1684
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));
}
Upvotes: 1
Views: 14267
Reputation: 186
here is how i did it in 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;
}
I create a function called fillArray
which uses memcpy
to copy a specified element into each slot of the array. I now just put the corresponding element into the function parameter and print it out.
Upvotes: 0
Reputation: 461
My guess in the C99 would be below code:
#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 ;
}
}
In my opinion this is cleanest, safest solution.
Upvotes: 0
Reputation: 16441
Not pure C, but GCC allows you to do it quite nicely:
MyStruct arr[100] = { [ 0 ... 99 ] = { .a = 123, .b = 321 } };
If you need pure C, I'd follow Tri-Edge Al's answer.
Upvotes: 0
Reputation: 264391
The following should work in 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}};
Upvotes: 2
Reputation: 340
for(int i = 0; i < 100; i++)
{
arr[i] = abc;
}
Fastest and cleanest. The optimizer will most likely work it's magic too.
Upvotes: 2
Reputation: 42083
Be careful to type names of your types correctly (it's case sensitive) and don't forget the semicolon after the definition of your struct
, apart from these, your program should compile with no problems:
#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;
}
outputs 321
.
"How can it be done in fastest and simplest way?"
The simplest way would probably be using std::vector
and its appropriate constructor instead:
#include <vector>
void foo()
{
MyStruct abc;
abc.a = 123;
abc.b = 321;
std::vector<MyStruct> vec(100, abc);
...
}
Upvotes: 8