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,