CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
Aug 2009
Posts
68

## 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[10][50];
int y[50];```
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[5][0], x[5][1], x[5][2] ... x[5][50] to be equal to y[0], y[1], y[2] ... y[50]

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[5] = y;`
or

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

Any ideas?

Thanks!

2. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Simple array stuff

Originally Posted by karnavor
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[5][0], x[5][1], x[5][2] ... x[5][50] to be equal to y[0], y[1], y[2] ... y[50]

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[5][0];
std::copy(px, px + 50, y);```
Code:
```#include <cstdlib>
//...
int *px = &x[5][0];
memcpy( y, px, sizeof(int) * 50);```
Regards,

Paul McKenzie
Last edited by Paul McKenzie; August 24th, 2009 at 12:41 PM.

3. Member
Join Date
Aug 2009
Posts
68

## 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[0];
};

int main()
{
int** x;

// Want to assign x[5] 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!

4. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## 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.

5. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Simple array stuff

Originally Posted by karnavor
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[5] to be equal to the array that is pointed to by the return of CreateArray()
std::vector<int> y = CreateArray();

int *px = &x[5][0];
std::copy(px, px + y.size(), y.begin() );
}```
Regards,

Paul McKenzie
Last edited by Paul McKenzie; August 24th, 2009 at 01:40 PM. Reason: Correct last parameter of std::copy call

6. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## 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.)

7. Member
Join Date
Aug 2009
Posts
68

## Re: Simple array stuff

Thanks you guys thats a great help!!

8. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Simple array stuff

Originally Posted by Lindley
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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•