Reversing an array using pointers

Thread Starter

asilvester635

Joined Jan 26, 2017
73
I have this program that makes and populates an array. Then it is sent to a function called reverse, which reverses the order in the array. The compiler keeps giving errors. I'm not quite sure why.




CODE
Code:
void reverse(int* array, int size) {


   for (int i = 0; i < size/2; i++) {
       int temp = array[i];
       array[i] = array[size-i];
       array[size-i] = temp;
   } // end of for loop


} // end of reverse 




int main( int argc, char** argv ) {


   int array[8];


   // get and print size of the array
   int size = sizeof(array) / sizeof(array[0]);
   printf("Size is %d\n", size);


   // populate array
   for (int i = 0; i < size; i++) {
       array[i] = i;
   } // end of for loop


   // display array before reversing
   for (int i = 0; i < size; i++) {
       printf("%d ", array[i]);
   } // end of for loop


   // new line
   printf("\n");


   // reverse the array
   reverse(&array, size);


   // display the array again after reversing
   for (int i = 0;i < size; i++) {
       printf("%d ", array[i]);


   } // end of for loop
} // end of main



It keeps giving me this error
main.cc:17:14: error: indirection requires pointer operand ('int' invalid)
int temp = *array;
^~~~~~~~~
main.cc:18:3: error: indirection requires pointer operand ('int' invalid)
*array = *array[size-i];
^~~~~~~~~
main.cc:18:15: error: indirection requires pointer operand ('int' invalid)
*array = *array[size-i];
^~~~~~~~~~~~~~
main.cc:19:3: error: indirection requires pointer operand ('int' invalid)
*array[size-i] = temp;
^~~~~~~~~~~~~~
4 errors generated.
make: *** [main.o] Error 1
 

Thread Starter

asilvester635

Joined Jan 26, 2017
73
I removed the & in front of the array variable when I send it to reverse. Though it gave me what I think is an address location. Below is the output. Why is that? There is no print statement in my code that intentionally prints any address.

Output:
0 1 2 3 4 5 6 7
1412676568 7 6 5 4 3 2 1
[Finished in 0.5s]
 

JWHassler

Joined Sep 25, 2013
308
Seems that 'reverse()' has an indexing problem... everybody is shifted down one slot
'printf' has obediently displayed the value at array[0], which is wrong.
The compiler-error output does not refer to the source-code version posted(?)
 

MrAl

Joined Jun 17, 2014
13,680
Hi,

It looks like (real quick look) the loop setup for swapping is not set up right for an array.

If size=8 then the first items swapped are [0] and [8-0] which is just [0] and [8] when it should be [0] and [7] (which is [0] and [size-0-1]).

The entire swap set for size=8 would be:
0,7
1,6
2,5
3,4

thus the loop should go from 0 to 3 (k=0 to k<size/2) and the swap set would be size-k-1 not size-k.
You use 'i' instead of 'k' here that's all.

Checking:
0: size-0-1=7 (ok)
1: size-1-1=6 (ok)
2: size-2-1=5 (ok)
3: size-3-1=4 (ok)

Alternately you could decrement size first:
size=size-1; //will now be equal to 7
Now:
for (k=0;k<=size/2;k++) //note change to "<=" now
{
temp=array[k];
array[k]=array[size-k];
array[size-k]=temp;
}

See if that helps.
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,273
You had 3 main problems. I fixed it for you, and changed some variable names to try to help explain.

(1) You were confused between the size of memory required for the array, and the number of elements in the array. When you access elements in the array, you use the element index, not the byte offset.
(2) Second when you were reversing, don't forget that arrays are 0 based, meaning the first element is element 0, not element 1. So you have to add a -1 when you dereference an element.
(3) The name of an array is a pointer to the first element, in this case a pointer to int. So when you call your reverse function, which takes an int*, just use the name "array" to get the pointer (type int*) to the first element of the array.

Here's the working code, I hope this helps:

Code:
void reverse(int* array, int NumberArrayElements) {


  for (int i = 0; i < NumberArrayElements / 2; i++) {
    int temp = array[i];
    array[i] = array[NumberArrayElements - i - 1];    // -1 because the first element is element 0
    array[NumberArrayElements - i - 1] = temp;
  } // end of for loop


} // end of reverse




int main(int argc, char** argv) {

  const int NumberArrayElements = 8;
  int array[NumberArrayElements];


  // get and print size of the array
  int size = sizeof(int) * NumberArrayElements;
  printf("Size is %d\n", size);


  // populate array
  for (int i = 0; i < NumberArrayElements; i++) {
    array[i] = i;
  } // end of for loop


    // display array before reversing
  for (int i = 0; i < NumberArrayElements; i++) {
    printf("%d ", array[i]);
  } // end of for loop


    // new line
  printf("\n");


  // reverse the array
  reverse(array, NumberArrayElements);


  // display the array again after reversing
  for (int i = 0; i < NumberArrayElements; i++) {
    printf("%d ", array[i]);
  } // end of for loop

} // end of main
 
Top