c style array to std::array

Oct 23, 2016 at 1:51pm
how can i assign c style array to std::array?
1
2
int cstyle[3] = { 3,3,3 };
std::array<int,3> test = cstyle;
Oct 23, 2016 at 3:58pm
You can try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include<iostream>
#include<array>
using namespace std;

int main(){

int cstyle[3] = { 3,3,3 };
std::array<int,3>& test = reinterpret_cast<std::array<int,3>&>(cstyle);

for(auto& itr: test){
    cout<<itr<<"\n";
    }


}


Note that c-style array name reduces to a pointer and hence the std::array is of <int,3>& type. However this is considered to be a dirty and dangerous hack that should be avoided because if the internal implementation of std::array changes then this becomes error prone because the program, at the time of writing, assumes that both types of arrays have the same memory configuration
Oct 23, 2016 at 4:39pm
Hello elay,

You could try something simple like:
1
2
3
4
5
6
7
8
int cstyle[3] = { 1, 2, 3 };
std::array<int, 5> test{ 0 };  //  Initializes the entire array to 0.

for (int lp = 0; lp < 3; lp++)
	test[lp] = cstyle[lp];

for (int lp = 0; lp < 5; lp++)  //  Prints the whole array to show the changes made from previous loop.
	std::cout << "\n " << test[lp];


Hope that helps,

Andy
Oct 23, 2016 at 4:48pm
Or you could use std::copy to copy the cstyle array into the std::array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <array>

int main()
{
    int cstyle[3] = { 3, 33, 333 };
    std::array<int, 3> test;
    std::copy(cstyle, cstyle + 3, test.begin());

    for(auto& itr : test)
        std::cout << itr << std::endl;

    return 0;
}


Oct 23, 2016 at 5:01pm
OP mentions assigning one array to another but yes, the other solutions are also feasible if assignment is not strictly necessary
Oct 23, 2016 at 6:09pm
@gunnerfunner

If assignment is strictly necessary, then your solution is incorrect. You are not assigning to a variable of type array<int, N> from a regular array; you are initialising a reference of type array<int, N> to refer to a regular array.

@OP

You can't use operator= to set the value of a std::array<> to that of a C-style, you must copy the elements over, as Handy Andy and jlb have illustrated.

Andy
Oct 23, 2016 at 6:18pm
@andywestken: I'd put the reference (&) in bold to highlight that fact but thanks for reiterating it
Oct 23, 2016 at 6:43pm
I am picking up on your comment about "the other solutions are also feasible". To make it clear that your suggestion is not a solution to the original problem (and hence should be avoided.)
Oct 23, 2016 at 6:55pm
OK, if you want to be splitting hairs that's your problem but where exactly in my post do you see me claiming a solution?

It starts off with 'you can try', then the type with reference is described clearly in bold and finally there's a warning not to do this for various other reasons. Doesn't seem like a 'solution' to me, does it, but rather how far s/he could get with the assignment operator.

Remember, we're posting on a forum here, not drafting a legal document - so to copy and post a phrase from one post without context is very misleading and narrow-minded.
Topic archived. No new replies allowed.