# Thread: Copying from one array to another...

1. ## Copying from one array to another...

G'day! Just a little background, I'm a first year student who is just starting to learn C89 (Yes, I have to use C89). I learned a little Java earlier this year, and am now learning C89 and more Java at the same time.

The following code is supposed to put an array into reverse order. I'm creating a temporary array, and then trying to copy all the values back into the original array, to go back to the function calling this fuction.

I get a segmentation fault just after it prints "DEBUG". On line 19 I'd imagine.

Could someone enlighten me as to what I'm doing wrong?

Code:
```
void reverse( int array[], int length)
{
int ii, temp;
int jj = 0;
int *tempArray = (int)malloc( length * sizeof( int));

for ( ii = length-1; ii >= 0; ii--)
{
tempArray[jj] = array[ii];
jj += 1;
}

array = *tempArray;

for (ii = 0; ii <= length-1; ii++)
{
printf("DEBUG\n");

temp = tempArray[ii];
array[ii] = temp;

}

free( tempArray);
}```
thanks,

2. Originally Posted by The Doctor
The following code is supposed to put an array into reverse order. I'm creating a temporary array, and then trying to copy all the values back into the original array, to go back to the function calling this fuction.
Instead of creating a temporary array, think of how you could swap the first element with the last element, the second element with the second last element, etc, to reverse the array in-place.

Anyway, this is wrong:
Code:
`array = *tempArray;`
array is a pointer to int, *tempArray is an int, so assigning one to the other does not make sense here.

3. Thanks! Actually, on looking at it again, I don't actually need that line. The for loop does what that line was meant to. (and it works...)

I actually tried to do it in place originally, but a fellow student kept pointing out flaws in my code, and I wasn't sure how to fix them at the time.

By the way, when I compile, It comes up with these warnings:

manage_array.c: In function ‘reverse’:
manage_array.c:42:19: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
manage_array.c:42:19: warning: initialization makes pointer from integer without a cast [enabled by default]
(There's more functions in manage_array.c, as you can probably guess...

Line 42 is my malloc statement.

Code:
```void reverse( int array[], int length)
{
int ii, temp;
int jj = 0;
int *tempArray = (int)malloc( length * sizeof( int));

for ( ii = length-1; ii >= 0; ii--)
{
tempArray[jj] = array[ii];
jj += 1;
}

for (ii = 0; ii <= length-1; ii++)
{

temp = tempArray[ii];
array[ii] = temp;

}

free( tempArray);
}```

4. Originally Posted by The Doctor
I actually tried to do it in place originally, but a fellow student kept pointing out flaws in my code, and I wasn't sure how to fix them at the time.
Then see if you now are able to fix those flaws

Originally Posted by The Doctor
By the way, when I compile, It comes up with these warnings:
Yeah, you should not be casting the return value of malloc. You casted it incorrectly, i.e., if you did want to cast, you should cast to int* not int.

Also, instead of writing ii <= length-1 just write ii < length. You don't need the temp variable (though it would come it handy for the in-place reverse).

5. int*tempArray = (int *)malloc( length * sizeof( int));

6. Derp, yes of course... Thanks guys!

7. Originally Posted by Click_here
int*tempArray = (int *)malloc( length * sizeof( int));
I recommend:
Code:
`int *tempArray = malloc(length * sizeof(*tempArray));`

8. Originally Posted by laserlight
I recommend:
Code:
`int *tempArray = malloc(length * sizeof(*tempArray));`

But wait, why would we be using the sizeof(*tempArray)? I don't get it. Aren't we just defining *tempArray there? Do you mean the size of the other array?

9. Originally Posted by The Doctor
why would we be using the sizeof(*tempArray)?
The idea is that all the elements of the array are of the same size, so we take the size of the first element, i.e., sizeof(*tempArray). Granted, the dynamic array does not exist yet, but that is not a problem because sizeof is evaluated at compile time here.