0

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];

1
  • 3
    Using std::array or std::vector will give you the syntax you're looking for, and better in the future. Commented May 1, 2012 at 13:22

2 Answers 2

5

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));
Sign up to request clarification or add additional context in comments.

3 Comments

@LuchianGrigore, I thought it did.
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.
@hmjd: Or use boost::begin and boost::end (or just boost::copy which is better than std::copy anyway).
3

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.