You can't directly do array2 = array1, because in this case you manipulate the addresses of the arrays (char *) and not of their inner values (char).
What you, conceptually, want is to do is iterate through all the chars of your source (array1) and copy them to the destination (array2). There are several ways to do this. For example you could write a simple for loop, or use memcpy.
That being said, the recommended way for strings is to use strncpy. It prevents common errors resulting in, for example, buffer overflows (which is especially dangerous if array1 is filled from user input: keyboard, network, etc). Like so:
Copy// Will copy 18 characters from array1 to array2
strncpy(array2, array1, 18);
As @Prof. Falken mentioned in a comment, strncpy can be evil. Make sure your target buffer is big enough to contain the source buffer (including the \0 at the end of the string).
You can't directly do array2 = array1, because in this case you manipulate the addresses of the arrays (char *) and not of their inner values (char).
What you, conceptually, want is to do is iterate through all the chars of your source (array1) and copy them to the destination (array2). There are several ways to do this. For example you could write a simple for loop, or use memcpy.
That being said, the recommended way for strings is to use strncpy. It prevents common errors resulting in, for example, buffer overflows (which is especially dangerous if array1 is filled from user input: keyboard, network, etc). Like so:
Copy// Will copy 18 characters from array1 to array2
strncpy(array2, array1, 18);
As @Prof. Falken mentioned in a comment, strncpy can be evil. Make sure your target buffer is big enough to contain the source buffer (including the \0 at the end of the string).
If your arrays are not string arrays, use:
memcpy(array2, array1, sizeof(array2));
Why in C arrays can be copied one element at a time using loop but entire array cannot be copied at one stroke
Copying Array into another array
c - Fast way to copy an array - Stack Overflow
Is there a function to copy an array in C/C++? - Stack Overflow
Videos
It will help to know explanation of the reason why in C arrays can be copied one element at a time using loop but entire array cannot be copied at one stroke.
So below seems correct way to copy array:
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copyimage[height][width];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
copyimage[y][x] = image[y][x];
}
}But not:
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copyimage[height][width] = image[height][width];
}
Since the memory size of your array is fixed, you can simply copy the memory block from one pointer to the other. It doesn't get any faster than that.
In c/c++ you could use memcpy if that is the language you are using. Every language has something equivalent.
Edit: since you confirmed use of c I can get more detailed:
memcpy(array_new,array,sizeof(VARIABLE_TYPE_of_ARRAY_ELEMENT)*N_a*N_b);
Your pointer-swap code is just a little bit off: you are dereferencing your pointers where you shouldn't. After all, the point of that code is to avoid copying data by just swapping two pointers. Here are the correct versions (depending on whether you use a true 2D array or an array of pointers to arrays):
//array is declared as
double (*array)[N_b];
double (*temp)[N_b] = array;
array = array_new;
array_new = temp;
or
//array is declared as
double** array;
double** temp = array;
array = array_new;
array_new = temp;
This is all you need, and it's definitely the fastest possible way to exchange contents of two buffers. Much faster than memcpy()...
Since you asked for a C++ solution...
#include <algorithm>
#include <iterator>
const int arr_size = 10;
some_type src[arr_size];
// ...
some_type dest[arr_size];
std::copy(std::begin(src), std::end(src), std::begin(dest));
Since C++11, you can copy arrays directly with std::array:
#include <array>
std::array<int, 4> A = {10, 20, 30, 40};
std::array<int, 4> B = A; // Copy array A into array B
Not sure what you are trying to do with this declaration but in frist place to declare array you need something like :
char arr1[1000][3];
char arr1[0][0] = 'A';
char arr1[0][0] = 'B';
...
And in order to copy each element one at a time use a for loop:
if ( something that determines X ? ) {
for ( int i=0; i < 3 ; i++ ) {
arr2[x][i] = arr1[x][i];
}
}
I would do it like this:
int i;
char arr1[1000][3];
char arr2[1000][3];
arr1[0][0]='v';
for(i=0;i<1000;++i)
{
strncpy(arr2[i],arr1[i],3); //safe copy, copies max. 3 chars.
}
printf("%c\n",arr2[0][0]);