I want to assign one row of 2d array to 1d array this is what i want to do.
int words[40][16];
int arrtemp[16];
arrtemp=words[i];
2 Answers
Use std::copy:
int words[40][16]; //Be sure to initialise words and i at some point
int arrtemp[16];
//If you don't have std::begin and std::end,
//use boost::begin and boost::end (from Boost.Range) or
//std::copy(words[i] + 0, words[i] + 16, arrtemp + 0) instead.
std::copy(std::begin(words[i]), std::end(words[i]), std::begin(arrtemp));
3 Comments
Qaz
@LuchianGrigore, I thought it did.
hmjd
std::begin() and std::end() where added in C++11, but you could use std::copy(words[0], words[0] + 16, arrtemp); in pre C++11.Mankarse
@hmjd: Or use
boost::begin and boost::end (or just boost::copy which is better than std::copy anyway).Arrays are immutable in C and C++. You can't reassign them.
You can use memcpy:
memcpy(arrtemp, words[i], 16 * sizeof(int) );
This copies 16 * sizeof(int) bytes from words[i] to arrtemp.
1 Comment
Benjamin Lindley
That's a strange use of the word immutable. I've only ever heard the word immutable used to describe objects whose contents can't change.
std::arrayorstd::vectorwill give you the syntax you're looking for, and better in the future.