I'm trying to write some code that will take each individual part of a 2D array and put it into the same part of a different array. Here's the code, tell me what you think!! ^_^ Thanks in advance for any help!!

``````bool repeat = true;
int a = 0;
int b = 0;
char array1[10][10];
char array2[10][10];

while (repeat == true)
{
if (a < 10)
{
if (b < 10)
{
array1[a][b] = array2[a][b];
b = b + 1;
// I'v compiled this with a cout << a << b; right here and it always says that a is
// 0 and that b is 1 at this point
if (b == 10)
{
b = 0;
a = a + 1;
}
}
if (a == 10)
{
repeat = false;
}
}
}``````

Sorry, if this seems incomplete, it's from a function in another program. Please help me if you have any idea why this doesn't work!! Thanks ^_^!!

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by zhelih

why don't you just use nested for-next loops?

``````int a = 0;
int b = 0;
char array1[10][10];
char array2[10][10];
for(a = 0; a < 10; ++a)
{
for( b = 0; b < 10; ++b)
{
array1[a][b] = array2[a][b];
}
}``````

or an even easier way to do it is like this:

``````char array1[10][10];
char array2[10][10];

// copy array1 into array2
memcpy(array2, array1, sizeof(array1));``````

Thanks for the correction of the code syntax and I'll try these and get back to you!! =]

Thanks, it worked perfectly, but what is the sizeof function?

Thanks, it worked perfectly, but what is the sizeof function?

It's not a function. It is a C++ operator, that return the size of variable or type in bytes.

E.G.

int i[5];
sizeof(i);
sizeof(int);
sizeof(double);
sizeof(i[0]);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.