 # Simple array stuff

Printable View

• August 24th, 2009, 10:34 AM
karnavor
Simple array stuff
Hello,

Here's a simple question regarding arrays that I probably ought to know...

If I have a 2-dimensional, 10 by 50, integar array, and another one-dimensional integar array

Code:

```int x; int y;```
How can I assign one of the arrays in x, to be equal to y?

For example, say y has been filled with a load of values, and I now want to assign the fifth array (of the first dimension) of x to be equal to the y array. I want to set x, x, x ... x to be equal to y, y, y ... y

I know that I could do this by using a loop and setting every element individually, but is there a way of doing this all at once?

Something like this:

Code:

`x = y;`
or

Code:

`x[] = y;`
But neither of these work....

Any ideas?

Thanks!
• August 24th, 2009, 10:48 AM
Paul McKenzie
Re: Simple array stuff
Quote:

For example, say y has been filled with a load of values, and I now want to assign the fifth array (of the first dimension) of x to be equal to the y array. I want to set x, x, x ... x to be equal to y, y, y ... y

I know that I could do this by using a loop and setting every element individually, but is there a way of doing this all at once?

Code:

```#include <algorithm> //... int *px = &x; std::copy(px, px + 50, y);```
Code:

```#include <cstdlib> //... int *px = &x; memcpy( y, px, sizeof(int) * 50);```
Regards,

Paul McKenzie
• August 24th, 2009, 11:14 AM
karnavor
Re: Simple array stuff
Ah yes, I see how that works, thanks. But, I have another problem now in that I don't actually know the size of y because it is dynamically allocated.

Basically, I have a function which creates my y array, and the return value of that function is a pointer to the y array. I then want to set the x array to be equal to the y array. But I don't know the length of the y array, and as such I don't know what the length of the second dimension of the x array should be...

Code:

```int* CreateArray() {     // Create the y array with dynamic allocation, and fill the elements of y     return &y; }; int main() {     int** x;         // Want to assign x to be equal to the array that is pointed to by the return of CreateArray()     return 0; }```
But if I don't know the length of y beforehand, then how can I do the memory copy that you suggest?

Thanks!
• August 24th, 2009, 11:41 AM
Lindley
Re: Simple array stuff
It is your responsibility to always have the size of a dynamically allocated array available in a variable somewhere. There's no reliable way to retrieve that information directly.

One common approach is to write the dynamic array in a class which stores both the array and its size. A well-tested implementation of this concept is std::vector.
• August 24th, 2009, 12:40 PM
Paul McKenzie
Re: Simple array stuff
Quote:

Ah yes, I see how that works, thanks. But, I have another problem now in that I don't actually know the size of y because it is dynamically allocated.

Use a container that knows the size of your "array", such as std::vector, as Lindley suggested.
Code:

```std::vector<int> CreateArray() {   int number_of_elements;   //...   //... assume you now know the number of elements   std::vector<int> y( number_of_elements) ;   // Use y just like an array   return y; }; int main() {     int** x;         // Want to assign x to be equal to the array that is pointed to by the return of CreateArray()   std::vector<int> y = CreateArray();   int *px = &x;   std::copy(px, px + y.size(), y.begin() ); }```
Regards,

Paul McKenzie
• August 24th, 2009, 12:50 PM
Lindley
Re: Simple array stuff
The final parameter of that copy probably needs to be y.begin() for now. (It may end up working like that in C++0x, once they better support the range concept in the STL.)
• August 24th, 2009, 01:24 PM
karnavor
Re: Simple array stuff
Thanks you guys thats a great help!!
• August 24th, 2009, 01:40 PM
Paul McKenzie
Re: Simple array stuff
Quote:

The final parameter of that copy probably needs to be y.begin() for now. (It may end up working like that in C++0x, once they better support the range concept in the STL.)

Yes, it should be y.begin(). I'll edit it to make the correction.

Regards,

Paul McKenzie