Alex Rivera | Logout

Initializing an array inside a structure from another array in C++

Asked 2013-03-05T09:25:19.587
9
struct sample {
    int x;
    int y;
    int arr[10];
};

int arr2[10] = {0, 1, 2, 4, 3, 2, 2, 1, 5, 5};

int a = 19; 
int b = 22;
struct sample* samp = new sample;
samp->x = a;
samp->y = b;
samp->arr = ??

In the above example, I need to initialize array inside the structure arr[10] with the elements of arr2[10].

How to do it in C++??

Edit
Report

1 Answer

2

you can use memcpy:

memcpy(sample->arr,arr2,sizeof(int) * 10)

But I would suggest using std::vector for both.

answered 2013-03-05T09:27:45.433

Your Answer